1 //adds a string type of simulation data. 2Jedis.set ("MyKey", "2");3Jedis.set ("Mykey2", "Hello");4 //Add simulation data for the set type. 5Jedis.sadd ("Mysetkey", "1", "2", "3");6 //adds a hash type of simulation data. 7Jedis.hset ("Mmtest", "username", "Stephen");8 9 //depending on the pattern in the parameter, all keys that conform to the pattern in the current database are obtained, as can be seen from the output, which does not distinguish between the value types associated with a key at execution time. Tenset<string> keys = Jedis.keys ("my*"); OneSystem.out.println ("keys=" +keys);//keys= [Mysetkey, Mykey2, MyKey] A - //removed two keys -Long del = Jedis.del ("MyKey", "Mykey2"); theSystem.out.println ("del:" +del);//Del:2 - - //Check to see if the key you just deleted still exists, and from the returned results, MyKey has indeed been deleted. -Boolean exists = jedis.exists ("MyKey"); +SYSTEM.OUT.PRINTLN (exists);//false - + //look at the key that has not been deleted to compare it to the command results above. ABoolean exists2 = jedis.exists ("Mysetkey"); atSystem.out.println ("exists2:" +exists2);//exists2:true - - //Move the Mysetkey key in the current database into the database with ID 1, which shows that the move was successful. -Long move = Jedis.move ("Mysetkey", 1); -System.out.println ("Move:" +move);//move:1 - in //Open the database with ID 1. -String select = Jedis.select (1); toSystem.out.println ("select:" +select);//Select:ok + - //Check to see if the key you just moved is there, and it already exists from the return result. theSystem.out.println (jedis.exists ("Mysetkey"));//true * $ //re-open the default database with ID 0. Panax NotoginsengString select2 = jedis.select (0); - the //Check to see if the key that has just been removed is no longer present and is removed from the returned results. +System.out.println (jedis.exists ("Mysetkey"));//false A the //============== "", "" "" "" "" "" "" "" "" " + - //empties the currently selected database $ Jedis.flushall (); $Jedis.set ("MyKey", "Hello"); - //rename MyKey to Mykey1 -String rename = Jedis.rename ("MyKey", "Mykey1"); theSystem.out.println ("rename:" +rename);//Rename:ok - Wuyi //since MyKey has been renamed, fetching again will return null theSystem.out.println (Jedis.get ("MyKey"));//NULL - Wu //get by the new key name -System.out.println (Jedis.get ("Mykey1"));//Hello About $ //the error message is returned because the MyKey no longer exists. -String rename2 = Jedis.rename ("MyKey", "Mykey1"); -System.out.println ("Rename2:" +rename2); - //Redis.clients.jedis.exceptions.JedisDataException:ERR no such key A + theJedis.set ("Oldkey", "Hello"); -Jedis.set ("Newkey", "World"); $ the //the command did not execute successfully because Newkey already exists theLong Renamenx = Jedis.renamenx ("Oldkey", "Newkey"); theSystem.out.println ("Renamenx:" +renamenx);//renamenx:0 the - //look at the value of the Newkey and find that it has not been renamenx overwritten. inSystem.out.println (Jedis.get ("Newkey"));// World the the About //=================>>>>>>>> the //empties the currently selected database the Jedis.flushall (); the +Jedis.set ("MyKey", "Hello"); - the //set the time-out for the key to 100 seconds. BayiLong expire = Jedis.expire ("MyKey", 100); theSystem.out.println ("Expire:" +expire);//expire:1 the - //See how many seconds are left with the TTL command. -Long ttl = Jedis.ttl ("MyKey"); theSystem.out.println ("TTL:" +ttl);//ttl:98 the the //execute the PERSIST command immediately, the timeout key becomes the persistent key, the timeout of the key is removed. theLong persist = jedis.persist ("MyKey"); -System.out.println ("Persist:" +persist);//persist:1 the the //The return value of the TTL tells us that the key has not timed out. theSystem.out.println (Jedis.ttl ("MyKey"));//-194 the //prepares the data for the subsequent expire command. theJedis.del ("MyKey"); the 98Jedis.set ("MyKey", "Hello"); About - //The timeout for setting the key is 100 seconds. 101Jedis.expire ("MyKey", 100);102 103 //use the TTL command to see how many seconds are left, and from the results you can see that there are 96 seconds left. 104System.out.println (Jedis.ttl ("MyKey"));// the the 106 //The time-out for re-updating the key is 20 seconds, and the return value shows that the command executed successfully. 107System.out.println (Jedis.expire ("MyKey", 20));//1108 109 //then use the TTL to confirm, from the results can be seen as sure enough to be updated. theSystem.out.println (Jedis.ttl ("MyKey"));// -111 the //immediately update the value of the key so that its time-out is invalid. 113System.out.println (Jedis.set ("MyKey", "World"));//OK the the //#从ttl的结果可以看出, after the last command that modified the key was executed, the timeout for that key was not valid. theSystem.out.println (Jedis.ttl ("MyKey"));//-1117 118 //================>>>>>>>>>>>>>>>>>119 - //View the data type of a key121 //because the MM key does not exist in the database, the command returns none122String type = Jedis.type ("MM");123System.out.println (type);//None124 theString type2 = Jedis.type ("MyKey");126System.out.println ("type2=" +type2);//type2= String127 - //prepares a value that is a key of the set type129Jedis.sadd ("Mysetkey", "1", "2"); the 131 //the Mysetkey key is set, so the string set is returned. theString type3 = Jedis.type ("Mysetkey");133System.out.println ("type3=" +type3);//type3= Set134 135 //returns any key in the database. 136String Randomkey =Jedis.randomkey ();137System.out.println ("randomkey=" +randomkey);//randomkey= MyKey This value is random.138 139 //empties the currently open database. $ Jedis.flushall ();141 142 143 //returned null due to no data144System.out.println (Jedis.randomkey ());//NULL145
Using the Jedis operation redis-the operation of the Java language in the client operation---to key