1.Redis Introduction
Redis is a key-value storage system. Similar to memcached, but solves the situation where the data is completely lost after a power outage, and she supports more untyped value types, in addition to string, supports lists (linked list), sets (collection), and Zsets (ordered collection) data types. These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic.
2.Performance of Redis
Official data: 110,000 can be saved per second, 81,000 per second is desirable.
3. Installing Redis
The Redis code follows ansi-c writing, and the system (such as Linux, etc.) is installed on the run. And Redis does not rely on any nonstandard libraries, and no compilation parameters are required to be added.
First go to the official website to download the source:
wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz
Extract:
TAR–ZXVF redis-2.4.6.tar.gz
Compile
To illustrate, the installation of Redis is very simple, there is already a ready-made makefile file, run the make command directly.
Make
Make install
Redis consists of four executables:redis-benchmark,redis-cli,redis-server,redis-stat four files, plus a The redis.conf constitutes the final available package for the entire redis. Their role is as follows:
- Redis-server:redis Server Daemon Startup program
- Redis-cli:redis command-line operation tool. Of course, you can also use Telnet to operate on its plain text protocol.
- Redis-benchmark:redis Performance testing tools to test the read and write performance of Redis in your system and in your configuration
- Redis-stat:redis Status Detection Tool to detect Redis current status parameters and delay status
Redis can now be started, and Redis has only one boot parameter, which is his profile path.
Redis-server/etc/redis.conf
Note that the daemonize parameter for the default copy of the past redis.conf file is no, so Redis does not run in the background, so we need to reopen a terminal to test it. Modify to Yes to run Redis in the background. In addition, the configuration file specifies the address of the PID file, log file and data file, if necessary, the default log information is directed to stdout.
The following are the meanings of the main configuration parameters of redis.conf:
- Daemonize: Whether to run daemon mode later
- Pidfile:pid File Location
- Port: Port number for listening
- Timeout: Request time-out
- Loglevel:log Information level
- Logfile:log File Location
- Databases: number of open databases
- Save *: How often the snapshot is saved, the first * indicates how long, and the third * indicates how many times the write operation is performed. Snapshots are automatically saved when a certain number of writes are performed within a certain amount of time. You can set multiple conditions.
- Rdbcompression: Whether to use compression
- Dbfilename: Data Snapshot file name (only file name, excluding directory)
- Dir: Save directory for Data snapshot (this is the directory)
- AppendOnly: If the appendonlylog is turned on, each write will record a log, which will improve the data anti-risk ability, but affect the efficiency.
- Appendfsync:appendonlylog How to sync to disk (three options, each write is forced to call Fsync, Fsync per second, do not call Fsync wait for the system to synchronize itself)
At this point you can open a terminal for testing, the default listening port in the configuration file is 6379
We can open a Redis client for testing
[Email protected] ~]# REDIS-CLI
Could not connect to Redis at 127.0.0.1:6379:connection refused
Not connected> exit
[Email protected] ~]# redis-server/etc/redis.conf
[Email protected] ~]# REDIS-CLI
Redis 127.0.0.1:6379> quit
4. Common operation
Call the Redis API through Java for storage and querying:
Import Com.agd.utils.propertiesutils;import Redis.clients.jedis.jedispool;import redis.clients.jedis.jedispoolconfig;/** * Redis Connection Pool * < A word function description > * < function Details > * * @author khj * @see [Related Classes/methods] * @since [Product/module version] */public class Redisclientpool{ public static Redisclientpool Redisclientpool = getinstance (); public static Jedispool jedispool; public static synchronized Redisclientpool getinstance () { if (null = = Redisclientpool) { Redisclientpool = new Redisclientpool (); } return redisclientpool; } Public Redisclientpool () &nbsP { if (null = = Jedispool) { Init (); } } /** * Initialize Jedis * < A word function summary > * < function Details > * @ return * @see [Class, Class # method, Class # member] */ private static Jedispoolconfig initpoolconfig () { jedispoolconfig Jedispoolconfig = new Jedispoolconfig (); //controls the maximum number of Jedis instances in a pool that have a status of idle Jedispoolconfig.setmaxidle (; ) //Maximum number of objects that can remain idle jedispoolconfig.setmaxidle (300);nbsp; //Time-out Jedispoolconfig.setmaxwaitmillis (+); //When borrow a Jedis instance, Whether the alidate operation is performed in advance, and if true, the resulting Jedis instance is available; Jedispoolconfig.settestonborrow (TRUE); //Whether the validate operation is performed in advance when the pool is also given Jedispoolconfig.settestonreturn (True); return jedispoolconfig; } /** * Initialize Jedis connection pool */ public static void Init () { jedispoolconfig Jedispoolconfig = Initpoolconfig (); String host = Propertiesutils.getvalue ("Redis.host");//"localhost"; int port = integer.parseint (Propertiesutils.getvalue ("Redis.port"));//6379; int timeout = integer.parseint (Propertiesutils.getvalue ("Redis.timeout"));//60000; //Fabric connection pool Jedispool = new Jedispool ( Jedispoolconfig, host, port, timeout); }
Import Java.util.list;import Java.util.map;import Java.util.concurrent.concurrenthashmap;import redis.clients.jedis.jedis;/** * Redis Operations api * < A word function description > * < function details > * * @ author khj * @see [Related Classes/methods] * @since [Product/Module version] */public class Redisclient { /** * Save data type map * < A quote feature brief > * < function description > * @param flag * @param mapdata * @see [Class, Class # method, Class # member] */ public static void Setmapdatatoredis (String flag,map<string,string> mapData) { Jedis redisclient = null; try { redisclient = RedisClientPool.jedisPool.getResource (); Redisclient.hmset (flag,mapdata); } catch (Exception e) { //Destroying Objects RedisClientPool.jedisPool.returnBrokenResource (redisclient); } finally { //Restore to Connection pool RedisClientPool.jedisPool.returnResource (redisclient); } } /** * Save Data &NBsp; type is key-value * < a sentence function description > * < function Details > * @param flag * @param field * @param value & nbsp; * @see [Class, Class # method, Class # member] */ public static void Setdatatoredis ( String flag,string field,string value) { Jedis Redisclient = null; try { redisclient = RedisClientPool.jedisPool.getResource (); Redisclient.hset (flag, field, value); } catch (Exception e) {   Destroying Objects RedisClientPool.jedisPool.returnBrokenResource (redisclient); } finally { //Restore to Connection pool RedisClientPool.jedisPool.returnResource (redisclient); } } /** * get map data * < A word function description > * < function Details > * @param flag * @return * @see [Class, Class # method, Class # member] */ public static map<string,string> Getmapdata (String flag) { map<string,string> DataMap = null; Jedis redisclient = null; try { redisclient = RedisClientPool.jedisPool.getResource (); DataMap = Redisclient.hgetall (flag); } catch (Exception e) { //Destroying Objects RedisClientPool.jedisPool.returnBrokenResource (redisclient); } finally { //Restore to Connection pool RedisClientPool.jedisPool.returnResource (redisclient); } return datamap; } public Static long DeleteData (String flag) { long result = 0; Jedis redisclient = null; Try { Redisclient = RedisClientPool.jedisPool.getResource (); result = Redisclient.del (flag); } catch (Exception e) { //Destroying Objects RedisClientPool.jedisPool.returnBrokenResource (redisclient); } finally { //Restore to Connection pool RedisClientPool.jedisPool.returnResource (redisclient); } return result; } /** * Retrieve data based on key and field * < A word function Description > * < function Details > * @param flag * @param field * @return * @see [Class, Class # method, Class # member] * / public static string GetData (String flag,string field) { & nbsp; String data = null; Jedis redisclient = null; & nbsp; try { redisclient = RedisClientPool.jedisPool.getResource (); data = Redisclient.hget (flag, field); } catch (Exception e) { //Destroy Objects RedisClientPool.jedisPool.returnBrokenResource (redisclient); } finally { //Restore to Connection pool RedisClientPool.jedisPool.returnResource ( redisclient); } return data; } public static void Main (string[] args) throws exception { Redisclient.testmap (); } public void testlist () { Jedis Redis = RedisClientPool.jedisPool.getResource ( ); //hset key field value sets the value of the field field in the hash table key to values. Redis.hset ("TablE "," field1 "," value1 "); redis.hset ("table", "Field2", "value2"); redis.hset ("table", "field3", "value3"); //Returns the value of one or more given fields in a hash table key. list<string> List = redis.hmget ("table", "field1", "Field2", "field3 "); for (String tmp:list) { SYSTEM.OUT.PRINTLN (TMP); } } public static void TestMap () { //Set multiple Field-value (domain-value) pairs to the hash table key at the same time. map<string,string> Map = new concurrenthashmap<string,string > (); for (int i = 0;i < 10000;i++) { Map.put ("field" +i, "value" +i); } if (null! = GetData ("table", "Field1")) { deleteData ("table"); } //Get the value of username below Map map<string,string> maps = Getmapdata ("table"); System.out.println (Maps.size ()); Setmapdatatoredis ("table", map); //hgetall Key returns all the fields and values in the hash table key. maps = getmapdata ("table"); System.out.println (Maps.size ()); }}
The Java implementation of Redis storage and querying