ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;Importjunit.framework.TestCase;ImportOrg.junit.AfterClass;ImportOrg.junit.BeforeClass;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis; Public classTestredisextendsTestCase {PrivateJedis Jedis; @BeforeClass Public voidSetUp ()throwsexception{//Connect to a Redis server, 192.168.0.100:6379Jedis =NewJedis ("192.168.91.234", 6379); //Authority authentication//Jedis.auth ("admin"); } @AfterClass Public voidTearDown () {} @Test Public voidTestString ()throwsexception{//-----Add Data----------Jedis.set ("name", "Xinxin");//put the value-->xinxin into the Key-->nameJedis.set ("name", "Yueer");//overwrite the original data if it already existsJedis.setnx ("name", "Yueyue");//if it already exists, do not overwrite the original dataJedis.setnx ("Xingming", "Zhangdongyue");//if not present, increase this key value pairBoolean Xingis = jedis.exists ("Xing");//determine if the key value existsSystem.out.println ("Xing is exists:" +Xingis); System.out.println (Jedis.get ("Name")); System.out.println (Jedis.get ("Xingming")); Jedis.append ("Name", "Is My Lover");//StitchingSystem.out.println (Jedis.get ("name")); Jedis.del ("Name");//Delete a keySystem.out.println (Jedis.get ("name")); //set multiple key-value pairsJedis.mset ("name", "LiuLing", "Age", "all", "QQ", "476777XXX"); JEDIS.INCR ("Age");//to add 1 operationsSystem.out.println (Jedis.get ("name") + "-" + jedis.get ("age") + "-" + jedis.get ("QQ")); JEDIS.DECR ("Age");//perform minus 1 operationsSystem.out.println ("Minus 1 actions:" + jedis.get ("Age")); Jedis.flushdb (); //Clear DataSystem.out.println (Jedis.get ("xingming")); //set time-out unit secondsJedis.setex ("Expirtime", 1, "value"); System.out.println (Jedis.get ("Expirtime")); Thread.Sleep (1200); System.out.println (Jedis.get ("Expirtime")); } @Test Public voidTestlist ()throwsexception{jedis.flushdb (); //Clear DataJedis.rpush ("L_name", "Pan"); Jedis.rpush ("L_name", "Teng");//right-side insertionJedis.lpush ("L_name", "Name:");//Left Insert//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 alllist<string> values = Jedis.lrange ("L_name", 0, 1); System.out.println (values); System.out.println (Values.get (0)); //Array LengthSYSTEM.OUT.PRINTLN ("Array length:" + Jedis.llen ("L_name"))); //SortJedis.rpush ("L_num", "4"); Jedis.rpush ("L_num", "3"); Jedis.rpush ("L_num", "5"); Jedis.rpush ("L_num", "6"); System.out.println ("Sort:" + jedis.sort ("L_num"));//You can only sort the numbers//To modify a single value in a listJedis.lset ("L_name", 0, "name:"); Values= Jedis.lrange ("L_name", 0,-1); System.out.println ("Modify the values in the list:" +values); //Get list Specifies the value of the underlyingSYSTEM.OUT.PRINTLN ("Specify Subscript value:" + jedis.lindex ("L_name", 0)); //Delete data outside the intervalJedis.ltrim ("L_name", 0, 1); Values= Jedis.lrange ("L_name", 0,-1); System.out.println ("Delete resources outside the interval:" +values); //list out Stack//System.out.println (Jedis.lpop ("L_name")); //pop-up value on left sideSystem.out.println (Jedis.rpop ("L_name"));//pop-up value at right endValues = Jedis.lrange ("L_name", 0,-1); System.out.println ("List out of stack:" +values); //Delete the specified elementJedis.rpush ("l_elements", "Pan"); Jedis.rpush ("L_elements", "Pan"); Jedis.rpush ("L_elements", "zzz"); Jedis.rpush ("L_elements", "zzz"); Jedis.lrem ("L_elements", 0, "Pan");//removes all elements in the list that have a pan, the second parameter indicates how many are deleted, and 0 represents allValues = Jedis.lrange ("L_elements", 0,-1); System.out.println ("Delete element:" +values); } @Test Public voidTestset ()throwsexception{jedis.flushdb (); //Clear DataJedis.sadd ("MySet", "1"); Jedis.sadd ("MySet", "2"); Jedis.sadd ("MySet", "3"); Jedis.sadd ("MySet", "4"); Set<String> setvalues = jedis.smembers ("MySet"); System.out.println (setvalues); //Remove NonameJedis.srem ("MySet", "4"); System.out.println ("Delete Set element:" + jedis.smembers ("MySet")); //determine if 2 exists in setSystem.out.println (Jedis.sismember ("MySet", "4")); //Traverse all elementsset<string> Redisset = jedis.smembers ("MySet"); Iterator it=Redisset.iterator (); System.out.println ("Traverse Map"); while(It.hasnext ()) {System.out.println (It.next ()); } //set to find orthogonal differenceJedis Jedis2; Jedis2=NewJedis ("192.168.91.234", 6379); Jedis2.sadd ("Myset2", "2"); Jedis2.sadd ("Myset2", "3"); Jedis2.sadd ("Myset2", "6"); Jedis2.sadd ("Myset2", "7"); System.out.println ("Set intersection:" + jedis.sinter ("MySet", "Myset2")); System.out.println ("Collection jedis.sunion:" + ("MySet", "Myset2")); System.out.println ("Set Myset-myset2:" + Jedis.sdiff ("MySet", "Myset2")); System.out.println ("Set Myset2-myset:" + Jedis.sdiff ("Myset2", "MySet")); } @Test Public voidTestsortset () {jedis.flushdb (); Jedis.zadd ("Sort_set", 1, "first"); Jedis.zadd ("Sort_set", 4, "fourth"); Jedis.zadd ("Sort_set", 2, "second"); Jedis.zadd ("Sort_set", 3, "third"); Jedis.zadd ("Sort_set", 5, "fifth"); Jedis.zadd ("Sort_set", 6, "sixth"); Jedis.zadd ("Sort_set", 7, "seventh"); Jedis.zadd ("Sort_set", 8, "eighth"); Jedis.zadd ("Sort_set", 8, "Eighth2"); Jedis.zadd ("Sort_set", 9, "Nineth"); Jedis.zadd ("Sort_set", 0, "zero"); System.out.println ("Ordered set:" + Jedis.zrange ("Sort_set", 0,-1)); //to delete an element in a collectionJedis.zrem ("Sort_set", "third"); System.out.println ("Ordered collection Delete element:" + jedis.zrange ("Sort_set", 0,-1)); //How many elements are counted?System.out.println ("Number of ordered set elements:" + jedis.zcard ("Sort_set"))); //take an element in a rangeSystem.out.println ("Gets the element based on the starting End index:" + jedis.zrange ("Sort_set", 0, 1));//Start index End Index//number of elements within the statistical weight rangeSystem.out.println ("Number of elements in the statistical weight range:" + jedis.zcount ("Sort_set", 7,8)); //View the weight of an elementSystem.out.println ("View weight of element:" + jedis.zscore ("Sort_set", "eighth")); //} @Test Public voidTesthashmap () {jedis.flushdb (); Map<String,String> map =NewHashmap<string,string>(); Map.put ("Name", "Panteng"); Map.put ("PWD", "123456"); Map.put ("Age", "24"); Jedis.hmset ("User", map); Jedis.hset ("User", "Address", "Beijing_haidian"); List<String> list = Jedis.hmget ("User", "name", "pwd", "address"); System.out.println ("Name=" + list.get (0)); System.out.println ("Password=" + list.get (1)); System.out.println ("Address=" + list.get (2)); //Delete a key value pairJedis.hdel ("User", "pwd"); //Get all keysSystem.out.println ("Get All keys in Hashs:" +jedis.hkeys ("user")); System.out.println ("Gets all the value in Hashs:" +jedis.hvals ("user")); }}
View Code
Redis-java Basic Operation