Summary
Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and on this basis implements the Master-slave (Master-slave ...). 0. Preface
Because it is the first use, so it is installed and used under Windows, refer to a few blog, the following to organize
1. Installing Redis
Official website: http://redis.io/
Official download: Http://redis.io/download can download different versions as needed
Windows Edition: Https://github.com/MSOpenTech/redis
GitHub resources can be downloaded directly from the zip (this is for students who do not know the friendship tips)
After the download is complete, you can right-click on a hard drive such as D:\Redis\redis-2.6
Under D:\Redis\redis-2.6\bin\release There are two zip packages, one 32-bit, one 64-bit
Extract the D:\Redis\redis-2.6 root directory according to the number of bits in your Windows
2. Start Redis
Turn on the service after entering the Redis directory (note plus redis.conf)
redis-server.exe redis.conf
The Redis service shuts down automatically when this window is to be turned on and off
Redis will automatically save the data to the hard drive so the picture is that I have one more DB loaded from disk when I start the second time.
3. Test use
Also open a command line window into the Redis directory (note to modify your own IP)
redis-cli.exe -h 192.168.10.61 -p 6379
4.Java Development Kit Jedis
Jedis:http://www.oschina.net/p/jedis (Redis's official Preferred Java Development Kit)
<!--Redis--<Dependency><GroupId>redis.clients</GroupId><Artifactid>jedis</Artifactid><Version>2.0.0</Version>< Type>jar</< Span class= "Hljs-tag" >type> <scope >compile</< Span class= "Hljs-tag" >scope> </dependency >
Test Example Original: http://flychao88.iteye.com/blog/1527163
Package com.lujianing.utils;Import Org.junit.Before;Import Org.junit.Test;Import Redis.clients.jedis.Jedis;Import Redis.clients.jedis.JedisPool;Import Redis.clients.jedis.JedisPoolConfig;Import Java.util.HashMap;Import Java.util.Iterator;Import java.util.List;Import Java.util.Map;/** * Created by lujianing on 14-2-28. */PublicClass Jedisutiltest{Jedispool Pool;Jedis Jedis; @Beforepublic void SetUp () {pool = newJedispool (NewJedispoolconfig (),"192.168.10.61"); Jedis = Pool.getresource ();Jedis.auth ("password"); } @Testpublic void Testget () {System.out.println (Jedis.Get"Lu")); }/** * Redis Storage Primary String * CRUD * *Testpublic void testbasicstring () {-----Add Data----------Jedis.Set"Name","MINXR");Put the VALUE-->MINXR into the Key-->nameSystem.out.println (Jedis.Get"Name"));Execution Result: MINXR-----Modify the Data-----------1, on the original basis to modify the Jedis.append ("Name","Jarorwar");Very intuitive, like the map will Jarorwar append to the already existing valueSystem.out.println (Jedis.Get"Name"));Execution Result: Minxrjarorwar2, directly cover the original data Jedis.Set"Name","Min Xiaorong");System.out.println (Jedis.Get"Name"));Execution Result: Min XiaorongDelete the key corresponding to the record Jedis.del ("Name");System.out.println (Jedis.Get"Name"));Execution result: null/** * Mset equivalent to * jedis.set ("name", "MINXR"); * Jedis.set ("Jarorwar", "Min Xiaorong"); */Jedis.mset ("Name","MINXR","Jarorwar","Min Xiaorong");System.out.println (Jedis.mget ("Name","Jarorwar")); }/** * Jedis Operation map */@Testpublic void TestMap () {map<String,String> user=newhashmap<String,String> (); User.put ("Name","MINXR"); User.put ("PWD","Password"); Jedis.hmset ("User", user);Remove the name from the user and execute the result:[minxr]--> note that the result is a generic listThe first parameter is the key to the Map object in Redis, followed by the key of the object placed in the map, followed by the key can be more than a variable parameterlist<string> Rsmap = Jedis.hmget ("User","Name");System.out.println (RSMAP);Delete a key value from a mapJedis.hdel ("User", "pwd");System.out.println (Jedis.hmget ("User","PWD"));Because it was deleted, NULL is returned.System.out.println (Jedis.hlen ("User"));Returns the number of values stored in the key for user 1System.out.println (Jedis.exists ("User"));Whether there is a record of key for user returns trueSystem.out.println (Jedis.hkeys ("User"));Returns all keys in the Map object [pwd, name]System.out.println (Jedis.hvals ("User"));Returns all the value in the Map object [MINXR, password]iterator<String> Iter=jedis.hkeys ("User"). iterator ();while (Iter.hasnext ()) {String key = Iter.next ();System.out.println (key+":" +jedis.hmget ("User", key)); } }/** * Jedis Operation list */@Testpublic void Testlist () {Before beginning, remove all content Jedis.del ("Java framework");System.out.println (Jedis.lrange ("Java framework",0,-1));First, store three data in the key Java framework Jedis.lpush ("Java framework","Spring"); Jedis.lpush ("Java framework","Struts"); Jedis.lpush ("Java framework","Hibernate");And then take out all the data jedis.lrange is out by range,The first one is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get allSystem.out.println (Jedis.lrange ("Java framework",0,-1)); }/** * Jedis Operation set */@Testpublic void Testset () {Add Jedis.sadd ("Sname","MINXR"); Jedis.sadd ("Sname","Jarorwar"); Jedis.sadd ("Sname","Min Xiaorong"); Jedis.sadd ("Sanme","Noname");Remove Noname Jedis.srem ("Sname","Noname");System.out.println (Jedis.smembers ("Sname"));Get all the added valueSystem.out.println (Jedis.sismember ("Sname","MINXR"));Determines whether MINXR is an element of the Sname collectionSystem.out.println (Jedis.srandmember ("Sname"));System.out.println (Jedis.scard ("Sname"));Returns the number of elements of the collection} @Testpublic void Test ()Throwsinterruptedexception {You can use wildcard characters to pass in keys.System.out.println (Jedis.keys ("*"));Returns all keys in the current library [Sose, Sanme, name, Jarorwar, foo, sname, Java Framework, user, Braand]System.out.println (Jedis.keys ("*name"));Returned sname [sname, name]System.out.println (Jedis.del ("Sanmdde"));Delete key for Sanmdde object Delete successfully returned 1 delete failed (or not present) returned 0System.out.println (Jedis.ttl ("Sname"));Returns the valid time for a given key, or 1 if it is always valid Jedis.setex ("TimeKey",10,"Min");This method allows you to specify the survival (active time) time of the key as secondsThread.Sleep (5000);After 5 Seconds of sleep, the remaining time will be <=5System.out.println (Jedis.ttl ("TimeKey"));The output results are 5 Jedis.setex ("TimeKey",1,"Min");After setting it to 1, let's see the remaining time is 1.System.out.println (Jedis.ttl ("TimeKey"));Output is 1System.out.println (Jedis.exists ("Key"));Check if key existsSystem.out.println (Jedis.rename ("TimeKey","Time"));System.out.println (Jedis.Get"TimeKey"));Returned as null because of removalSystem.out.println (Jedis.Get"Time"));The value min can be obtained because the TimeKey is renamed to TIMEJedis sortNote that the Rpush and Lpush here are the operations of the list. is a doubly linked list (but in terms of performance) Jedis.del ("A");First clear the data, then add the data to test Jedis.rpush ("A","1"); Jedis.lpush ("A","6"), Jedis.lpush ("A","3"), Jedis.lpush ("A","9"); System.out. println (Jedis.lrange ("A",0,-1)); [9, 3, 6, 1] System.out. println (Jedis. Sort ("a")); //[1, 3, 6, 9]//input sorted after result System.out. println (Jedis.lrange ("A",0,-1);}}
Redis will save data to the hard disk on a regular basis
Installation of Redis under Windows is used