I. Installation of Redis
1. Download the source code, unzip and compile the source code.
[Email protected] ~]# tar xzf redis-2.8.3.tar.gz
[Email protected] ~]# CD redis-2.8.3
[[email protected] ~]# make
2. Start the Redis service. (Src under redis-2.8.3)
[[Email protected] ~]# CD src
[[Email protected] ~]./redis-server
3. Connect a Redis Client
[Email protected] ~]#./redis-cli
4. Test, Redis IP is the machine's IP, the port default is 6379
127.0.0.1:6379> set ' hello ' ' test '
Ok
127.0.0.1:6379> Get Hello
"Test"
Two. Java Operation Redis
1. Connect to Redis directly and operate Redis
Package Com.sinovatech.mactivity.web.controller;import Java.util.hashmap;import Java.util.iterator;import Java.util.list;import java.util.map;import org.junit.before;import Org.junit.test;import Redis.clients.jedis.Jedis ;p Ublic class Testredisown {private Jedis Jedis; @Before public void Setup () {//Connect Redis server, 192.168.0.100:6379 Jedis = new Jedis ("10.143.131.63", 6378); Authority authentication//jedis.auth ("admin"); /** * Redis Storage String */@Test public void teststring () {//-----add Data----------jedis.se T ("name", "Xinxin");//Put Value-->xinxin System.out.println (jedis.get ("name") into key-->name;//Execution Result: xinxin Jedis.append ("name", "Is My Lover"); Stitching System.out.println (Jedis.get ("name")); Jedis.del ("name"); Delete a key System.out.println (Jedis.get ("name")); Set multiple key values to Jedis.mset ("name", "LiuLing", "Age", "Max", "QQ", "476777XXX"); JEDIS.INCR ("Age"); To add1 Operation System.out.println (Jedis.get ("name") + "-" + jedis.get ("age") + "-" + jedis.get ("QQ")); /** * Redis Operation MAP */@Test public void TestMap () {//-----add Data----------Map<stri Ng, string> map = new hashmap<string, string> (); Map.put ("name", "Xinxin"); Map.put ("Age", "22"); Map.put ("QQ", "123456"); Jedis.hmset ("user", map); Remove name from user, execute result:[minxr]--> Note that the result is a generic list//The 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 a variable parameter list<string> Rsmap = jedis.hmget ("User", "name", "Age", "QQ"); System.out.println (RSMAP); Delete a key value from the map Jedis.hdel ("User", "age"); System.out.println (Jedis.hmget ("User", "age"); Because it was deleted, the return is null System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 2 System.out.println (jedis.exists ("user"));//Whether there is a record of key for user returns true System.out.prin TLN (Jedis.hkeys ("user"));//returns all k in the Map objectEY System.out.println (jedis.hvals ("user"));//Returns all value iterator<string> Iter=jedis.hkeys in the Map object (" User "). iterator (); while (Iter.hasnext ()) {String key = Iter.next (); System.out.println (key+ ":" +jedis.hmget ("User", key)); }}/** * Jedis operation list */@Test public void Testlist () {//lpush inserts data from the head of the linked list, if the KE Y, create the empty linked list Jedis.lpush ("Java framework", "Spring") associated with the key before inserting; Jedis.lpush ("Java framework", "struts"); Jedis.lpush ("Java framework", "Hibernate"); Rpush inserts the data from the end of the linked list, and if there is no key, the empty list Jedis.rpush ("Java Frameworkr", "Spring") associated with the key is worn before insertion; Jedis.rpush ("Java Frameworkr", "struts"); Jedis.rpush ("Java Frameworkr", "hibernate"); Lpop, takes out the head element of the list, does not return null JEDIS.LPOP ("Java framework"); Rpop remove the trailing element of the list, key does not exist return null JEDIS.RPOP ("Java framework"); Then take out all the data jedis.lrange is out by range,//First is key, the second is the starting position, the third is the end bitSet, Jedis.llen get length-1 means get all System.out.println (Jedis.lrange ("Java framework", 0,-1)); }/** * Jedis operation set */@Test public void Testset () {//Add Jedis.sadd ("User", " LiuLing "); Jedis.sadd ("User", "xinxin"); Jedis.sadd ("User", "Ling"); Jedis.sadd ("User", "zhangxinxin"); Jedis.sadd ("User", "who"); Removal of Noname Jedis.srem ("user", "who"); System.out.println (jedis.smembers ("user"));//Get all added value System.out.println (jedis.sismember ("user", "who");//Award Whether the who is the element of the user collection System.out.println (jedis.srandmember ("user")); System.out.println (Jedis.scard ("user"));//Returns the number of elements of the collection} @Test public void Test () throws Interruptedexceptio n {//jedis sort//Note, here the Rpush and Lpush are the operations of the list. is a doubly linked list (but from the performance point of view) Jedis.del ("a");//Clear the data first, 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 results System.out.println (Jedis.lrange ("a", 0,-1)); }//The following method calls the connection pool in Redis @Test public void Testredispool () {Testredisutil.getjedis (). Set ("NewName", "Chinese Test"); System.out.println (Testredisutil.getjedis (). Get ("NewName")); } public static void Main () {Jedis jedis=testredisutil.getjedis (); Jedis.set ("testrediskey001", "testrediskey001"); String getvalue=jedis.get ("testrediskey001"); System.out.println (GetValue); }}
2. Create a Redis connection pool and operate using Redis
①redis Creating a connection pool
Package Com.sinovatech.mactivity.web.controller;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;public Final class TestRedisUtil {//Redis Server IP private static String ADDR = "10.143.131.63"; The port number of the Redis private static int port = 6378; Access password private static String AUTH = "admin"; The maximum number of available connection instances, the default value is 8, and//If the assignment is 1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. private static int max_active = 1024; Controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8. private static int max_idle = 200; The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the wait time is exceeded, the jedisconnectionexception is thrown directly; private static int max_wait = 10000; private static int TIMEOUT = 10000; Whether the validate operation is performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instance is available; private static Boolean test_on_borrow = true; private static Jedispool Jedispool = null; /** * Initializing Redis connection pool */static {try { Jedispoolconfig config = new Jedispoolconfig (); Config.setmaxidle (max_active); Config.setmaxidle (Max_idle); Config.setmaxwaitmillis (max_wait); Config.settestonborrow (Test_on_borrow); Jedispool = new Jedispool (config, ADDR, PORT); } catch (Exception e) {e.printstacktrace (); }}/** * Get Jedis instance * @return * */public synchronized static Jedis Getjedis () {try { if (Jedispool! = null) {Jedis resource = Jedispool.getresource (); return resource; } else {return null; }} catch (Exception e) {e.printstacktrace (); return null; }}/** * frees Jedis resource * @param Jedis */public static void Returnresource (final Jedis Jedis) { if (Jedis! = null) {Jedispool.returnresource (Jedis); } }}
② using a created Redis connection pool to operate Redis
Package Com.sinovatech.mactivity.web.controller;import Redis.clients.jedis.jedis;public class TestRedisOwnNew { public static void Main (string[] args) {//TODO auto-generated method Stubjedis Jedis=testredisutil.getjedis (); Jedis.set ("testrediskey001", "testrediskey001"); String getvalue=jedis.get ("testrediskey001"); System.out.println (GetValue); Jedis.del ("testrediskey001");} }
Three. How to use Redis
1.redis Storage string:
①//-----Add Data----------Call the Set method
Jedis.set ("name", "Xinxin");//Put Value-->xinxin in Key-->name
System.out.println (Jedis.get ("name"));//Execution Result: xinxin
Jedis.append ("name", "Is My Lover"); Stitching
System.out.println (Jedis.get ("name"));
②//Delete data, call the Del method
Jedis.del ("name"); Delete a key
System.out.println (Jedis.get ("name"));
③//set multiple key-value pairs, call the Mset method parameter format (key,value,key,value)
Jedis.mset ("name", "LiuLing", "Age", "all", "QQ", "476777XXX");
The ④//INCR method adds 1 to the value of a key, and if no key is created automatically, add 1
JEDIS.INCR ("Age"); To add 1 operations
System.out.println (Jedis.get ("name") + "-" + jedis.get ("age") + "-" + jedis.get ("QQ");
2. Redis Operation Map
①//-----Add Data----------Hmset method, put in a map
map<string, string> map = new hashmap<string, string> ();
Map.put ("name", "Xinxin");
Map.put ("Age", "22");
Map.put ("QQ", "123456");
Jedis.hmset ("user", map);
Remove the name from the user and execute the result:[minxr]--> note that the result is a generic list
The 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 parameter
②//fetch the data, call the Hmget method, the parameter is the key of the previously placed map, and the return is a list
list<string> Rsmap = jedis.hmget ("User", "name", "Age", "QQ");
System.out.println (RSMAP);
③//Delete a key value in the map, call the Hdel method, but delete multiple keys
Jedis.hdel ("User", "age");
System.out.println (Jedis.hmget ("User", "age"); Because it was deleted, NULL is returned.
System.out.println (Jedis.hlen ("user")); Returns the number of values stored in the key for user 2
System.out.println (jedis.exists ("user"));//Whether there is a record of key for user returns true
System.out.println (Jedis.hkeys ("user"));//returns all keys in the Map object
System.out.println (jedis.hvals ("user"));//returns all the value in the Map object
Iterator<string> Iter=jedis.hkeys ("user"). Iterator ();
while (Iter.hasnext ()) {
String key = Iter.next ();
System.out.println (key+ ":" +jedis.hmget ("User", key));
}
3.jedis Operation List
①//lpush inserts data from the head of the linked list, and if there is no key, creates an empty list associated with the key before inserting
Jedis.lpush ("Java framework", "Spring");
Jedis.lpush ("Java framework", "struts");
Jedis.lpush ("Java framework", "Hibernate");
②//rpush inserts the data from the end of the linked list, and if there is no key, the empty list associated with the key is worn before insertion
Jedis.rpush ("Java Frameworkr", "Spring");
Jedis.rpush ("Java Frameworkr", "struts");
Jedis.rpush ("Java Frameworkr", "hibernate");
③//lpop, takes out the head element of the list, does not return null
Jedis.lpop ("Java framework");
④//rpop remove the trailing element of the linked list, key does not exist return null
Jedis.rpop ("Java framework");
⑤//then take out all the data jedis.lrange is out by range, the first is key, the second is the starting position, the third is the end position, Jedis.llen gets the length-1 means get all
System.out.println (Jedis.lrange ("Java framework", 0,-1));
Using Jedis to operate Redis in Java