[Reserved] use Jedis operating redis

Redis is an open source Key-Value data cache, and Memcached similar.

Redis plurality of types of value, comprising a string (string), list (list), set (collection), zset (sorted set - ordered set) and hash (hash type).

Jedis are preferred Redis official Java client development package.

// connect redis, the default port is redis 6379 

Jedis jedis = new new Jedis ( "localhost", 6379 ); 



// authentication password, if a password is not set code omitted 

jedis.auth ( "password" ); 



jedis.connect ( ); // connected 

jedis.disconnect (); // disconnect 



the Set <String> = jedis.keys Keys ( "*"); // list all Key 

the Set <String> = jedis.keys Keys ( "Key "); // find a specific key 

 



// remove one or more of the given key, if the key does not exist, ignore the command. 

jedis.del ( " key1 " ); 

jedis.del ( " key1 "," key2 "," key3 "," key4 "," key5 "); 



// remove the given key to the survival time of (setting the key never expires)
jedis.persist ( "key1" ); 

// check whether the given key is present 
jedis.exists ( "key1" ); 

// be renamed newkey key, and when the same key or newkey key does not exist, an error is returned 
jedis. the rename ( "key1", "key2" ); 

// type of return value of the stored key. 
// none (Key does not exist), string (string), list (list), set (collection), zset (ordered set), hash (hash table) 
jedis.type ( "key1" ); 

// Set key survival time, when the key expires, it is automatically deleted. 
jedis.expire ( "key1",. 5); // . 5 seconds expire 
 


@ correlation value to the string value key. 
jedis.set ( "key1", "value1" ); 

// The value of the value associated with the key, and the key to the survival time seconds (s). 
jedis.setex ( "foo",. 5, "haha" );


jedis.flushAll (); 

// returns the number of key 
jedis.dbSize (); 

// value of domain hash table key field is set to value. 
jedis.hset ( "key1", "field1", "field1-value" ); 
jedis.hset ( "key1", "Field2", "Field2-value" ); 

the Map Map = new new the HashMap (); 
map.put ( "field1", "field1-value" ); 
map.put ( "Field2", "Field2-value" ); 
jedis.hmset ( "key1" , Map); 


// return a given domain field key in the hash table value 
jedis.hget ( "key1", "field1" );

( Int I = 0; I <list.size (); I ++ ) { 
   System.out.println (List.get (I)); 
} 

// return all fields in the hash table key values and 
Map <String, String> jedis.hgetAll = Map ( "key1" ); 
 for (entry of Map.Entry: EnumMap.entrySet ()) { 
   of System.out.print (entry.getKey () + ":" + entry.getValue () + "\ T " ); 
} 

// one or more specified domain deletion key in the hash table 
jedis.hdel (" key1 "," field1 " ); 
jedis.hdel ( " key1 "," field1 "," Field2 " ); 

/ / View the hash table key, a given domain field exists. 
jedis.hexists ( "key1", "field1" );



Returns the hash table key values of all 
jedis.hvals ( "key1" ); 



// the value of the key value inserted into the list header. 
jedis.lpush ( "key1", "value1-0" ); 
jedis.lpush ( "key1", "value1-1" ); 
jedis.lpush ( "key1", "value1-2" ); 

// returns a list of key elements within the specified range, the specified interval to start and stop offsets.
 // index (index) parameter start and stop start from 0;
 // negative index from the start of the representative (-1 is the last element of the list , -2 is the penultimate element of the list, and so forth) 
list list jedis.lrange = ( "key1", 0, -1); // STOP subscript values are within the range (inclusive) 
for ( int I = 0; I <list.size (); I ++ ) { 
   System.out.println (List.get (I)); 
} 

// Returns the length of the key list. 
jedis.llen ( "key1") 



// The member added to the collection of key elements among. 
jedis.sadd ( "key1", "value0," ); 
jedis.sadd ( "key1", "VALUE1" ); 

// remove the elements in the collection member. 
jedis.srem ( "key1", "value1" ); 

// returns the set key of all members. 
= Jedis.smembers SET SET ( "key1" ); 

// determines whether the element is a set of key members 
jedis.sismember ( "key1", "value2" )); 

// returns the number of elements set key 
jedis.scard ( " key1 " ); 
 
// returns all members of a collection, the collection is all to the intersection of a given set of 
jedis.sinter (" key1 "," key2 " ) 
 
// returns all members of a collection, the collection is all a given set the union 
jedis.sunion ( "


jedis.sdiff("key1","key2");

Reprinted Original:

https://www.cnblogs.com/relucent/p/4203190.html

Guess you like

Origin www.cnblogs.com/appium/p/11310980.html