Jedis slice (cluster) Non-slice (single-host) instance, jedis Cluster
Package testRedis;
Importjava. util. ArrayList;
Importjava. util. Iterator;
Importjava. util. List;
Importjava. util. Set;
Importredis. clients. jedis. Jedis;
Importredis. clients. jedis. JedisPool;
Importredis. clients. jedis. JedisPoolConfig;
Importredis. clients. jedis. JedisShardInfo;
Importredis. clients. jedis. ShardedJedis;
Importredis. clients. jedis. ShardedJedisPool;
Importredis. clients. jedis. SortingParams;
Importredis. clients. util. Pool;
/**
* @ Author WHD
* 2015-4-19
*/
Public classRedisClient {
// Obtain the database connection, non-slice Client Connection
PrivateJedis jedis;
// Non-slice connection pool
PrivateJedisPool jedisPool;
// Slice Client
PrivateShardedJedis shardedJedis;
// Slice connection pool
PrivateShardedJedisPool shardedJedisPool;
// Constructor
PublicRedisClient (){
// Initialize the connection pool
InitialPool ();
// Initialize the slice connection pool
InitialShardedPool ();
// Obtain the slice instance from the slice pool
ShardedJedis = shardedJedisPool. getResource ();
// Obtain an instance from a non-slice pool
Jedis = jedisPool. getResource ();
}
// Initialize a non-slice pool
Privatevoid initialPool (){
// Configure the pool
JedisPoolConfig config = new JedisPoolConfig ();
// Maximum number of connections
Config. setMaxActive (20 );
// Maximum number of idle connections
Config. setMaxIdle (5 );
// Obtain the maximum number of waiting milliseconds for a connection
Config. setMaxWait (10001 );
// Check validity when idle. The default value is false.
Config. setTestOnBorrow (false );
JedisPool = new JedisPool (config, "127.0.0.1", 6379 );
}
// Initialize the slice pool
Private void initialShardedPool (){
// Basic pool Configuration
JedisPoolConfig config = new JedisPoolConfig ();
// Maximum number of connections
Config. setMaxActive (20 );
// Maximum number of idle connections
Config. setMaxIdle (2 );
// Obtain the maximum number of waiting milliseconds for a connection
Config. setMaxWait (10001 );
// Check validity when idle. The default value is false.
Config. setTestOnBorrow (false );
// Slave connection. The cluster function is implemented here. Multiple redis services are configured to implement request allocation for load balancing.
List <JedisShardInfo> shards = newArrayList <JedisShardInfo> ();
Shards. add (newJedisShardInfo ("192.168.0.106", 6379, "master "));
Shards. add (newJedisShardInfo ("192.168.0.100", 6379, "master "));
// Construct the pool
ShardedJedisPool = newShardedJedisPool (config, shards );
}
Public void show (){
KeyOperate ();
StringOperate ();
ListOperate ();
// Release the connection
JedisPool. returnResource (jedis );
// Release the connection
ShardedJedisPool. returnResource (shardedJedis );
}
//
Private void KeyOperate (){
System. out. println ("Clear all databases" + jedis. flushDB ());
// Determine whether the key exists
System. out. println ("determining whether key999 exists" + shardedJedis. exists ("key999 "));
System. out. println ("New key001value001 key-Value Pair" + shardedJedis. set ("key001", "value001 "));
System. out. println ("determining whether key001 exists" + shardedJedis. exists ("key001 "));
// Output all keys in the system
System. out. println ("added key002value002" + shardedJedis. set ("key002", "value002 "));
System. out. println ("all keys in the System are as follows :");
Set <String> keys = jedis. keys ("*");
Iterator <String> it = keys. iterator ();
While (it. hasNext ()){
String key = it. next ();
System. out. println (key );
}
// Delete a key. If the key is not deleted, ignore it.
System. out. println ("delete key002" + jedis. del ("key002 "));
System. out. println ("determining whether there is" + jedis. exists ("key002 "));
// Set the expiration time of key 001
System. out. println ("set the key001 expiration time to five seconds" + jedis. expire ("key001", 5 ));
Try {
Thread. sleep (2000 );
} Catch (Exception e ){
E. printStackTrace ();
}
// Check whether the remaining time unit of a key does not exist in seconds or-1 is returned permanently
System. out. println ("view the remaining time of key001" + jedis. ttl ("key001 "));
// Time remaining for removing a key
System. out. println ("time remaining to delete key001" + jedis. persist ("key001 "));
// View the time after the removal
System. out. println ("Remaining Time" + jedis. ttl ("key001 "));
// View the type of the value stored by the key
System. out. println ("type of the value stored by the key" + jedis. type ("key001 "));
}
/**
* String type
*/
Private void StringOperate (){
System. out. println ("++ = ++ String ++ = ");
// Clear data
System. out. println ("Clear database data" + jedis. flushDB ());
System. out. println ("========= Zeng =========== ");
Jedis. set ("key001", "value001 ");
Jedis. set ("key002", "value002 ");
Jedis. set ("key003", "value003 ");
System. out. println ("to add three new key-value pairs :");
System. out. println (jedis. get ("key001 "));
System. out. println (jedis. get ("key002 "));
System. out. println (jedis. get ("key003 "));
System. out. println ("======= Delete ===== ");
System. out. println ("delete key003 key-value pairs" + jedis. del ("key003 "));
System. out. println ("get the corresponding value of key003" + jedis. get ("key003 "));
System. out. println ("========== ");
// Directly overwrite the original value
System. out. println ("overwrite the original value directly" + jedis. set ("key001", "value001-update "));
System. out. println ("get the new value of key001" + jedis. get ("key001 "));
// If yes, it will not be modified. If not, it will be modified.
System. out. println ("not added, otherwise it will be invalid" + jedis. setnx ("key001", "value001 a new "));
// Obtain the value to see
System. out. println ("after modification, it should be value001 a new to get this value" + jedis. get ("key001 "));
// Directly overwrite the original data
System. out. println ("append after the original value of ke0y002" + jedis. append ("key002", "appendString "));
System. out. println ("get new value of key002" + jedis. get ("key002 "));
System. out. println ("====== Zeng, delete multiple ====== ");
/**
* Mset mget also adds modifications to query multiple key-value pairs
*/
// Add multiple values at a time
System. out. println (jedis. mset ("key201", "value201", "key202", "value202", "key203", "value203", "key204", "value204 "));
// Obtain multiple created values at a time
System. out. println (jedis. mget ("key201", "key202", "key203", "key204 "));
// Delete multiple values at a time
System. out. println ("delete multiple values at a time" + jedis. del (newString [] {"key201", "key202", "key203", "key204 "}));
// Some jedis methods can also be used directly in shardedJedis. The following test shows some methods that have not been used before.
System. out. println ("========= STRING2 === ");
System. out. println ("Clear all databases" + jedis. flushDB ());
System. out. println ("add 301 when the original key301 does not exist" + shardedJedis. setnx ("key301", "value301 "));
System. out. println ("302 added when the original key302 does not exist" + shardedJedis. setnx ("key302", "value302 "));
System. out. println ("try to add 302 when the original key302 exists" + shardedJedis. setnx ("key302", "value302 "));
// Obtain the value of key3. ..
System. out. println ("get key301 value" + shardedJedis. get ("key301 "));
System. out. println ("get key302 value" + shardedJedis. get ("key302 "));
// Expired valid key-value pairs are deleted
System. out. println ("======= ");
System. out. println ("key303 is valid for 2" + shardedJedis. setex ("key303", 2, "key303-2second "));
System. out. println ("get key303 value" + shardedJedis. get ("key303 "));
Try {
Thread. sleep (3000 );
} Catch (Exception e ){
}
System. out. println ("expired key303 value" + shardedJedis. get ("key303 "));
// Obtain the original value and update it to the new value.
System. out. println ("original key302 value" + shardedJedis. getSet ("key302", "value302-aftergetset "));
System. out. println ("New Value of key302" + shardedJedis. get ("key302 "));
// Obtain the substring of a key
System. out. println ("obtain the substring in the corresponding value of key302" + shardedJedis. getrange ("key302", 2, 4 ));
}
/**
* List type
*/
Private void ListOperate (){
System. out. println ("====== list ===== 11 ");
// Clear data
System. out. println ("Clear all data in the database" + jedis. flushDB ());
// Add data
System. out. println ("======== ");
ShardedJedis. lpush ("stringlists", "vectings ");
ShardedJedis. lpush ("stringlists", "arraulist ");
ShardedJedis. lpush ("stringlists", "vectings ");
ShardedJedis. lpush ("stringlists", "vectings ");
ShardedJedis. lpush ("stringlists", "shortlist ");
ShardedJedis. lpush ("stringlists", "maplist ");
ShardedJedis. lpush ("stringlists", "hashlist ");
ShardedJedis. lpush ("numberlist", "1 ");
ShardedJedis. lpush ("numberlist", "2 ");
ShardedJedis. lpush ("numberlist", "3 ");
ShardedJedis. lpush ("numberlist", "4 ");
ShardedJedis. lpush ("numberlist", "5 ");
ShardedJedis. lpush ("numberlist", "6 ");
// Obtain all stringlist data
System. out. println ("all data stringlists" + shardedJedis. lrange ("stringlists", 0,-1 ));
List <String> stringlist = shardedJedis. lrange ("stringlists", 0,-1 );
System. out. println ("stringlist length" + stringlist. size ());
// Obtain all numberlist data
System. out. println ("retrieve all numberlist data" + shardedJedis. lrange ("numberlist", 0,-1 ));
List <String> numberlist = shardedJedis. lrange ("numberlist", 0,-1 );
System. out. println ("numberlist length" + numberlist. size ());
// Delete an element
System. out. println ("delete element ");
// Delete the value specified in the list. The second parameter is the number of deletions (if there are duplicates), and then the value is added and deleted first, similar to the stack
System. out. println ("delete specified element" + shardedJedis. lrem ("stringlists", 2, "vectings "));
System. out. println ("deleting specified elements in getting all elements" + shardedJedis. lrange ("stringlists", 0,-1 ));
// Delete data outside the specified range
System. out. println ("delete data outside the specified range" + shardedJedis. ltrim ("stringlists", 0, 3 ));
// Obtain and delete data outside the specified Element
System. out. println ("delete data in the specified range" + shardedJedis. lrange ("stringlists", 0,-1 ));
// List element output Stack
System. out. println ("list element output stack" + shardedJedis. lpop ("stringlists "));
// Obtain data after stack exit
System. out. println ("Get all elements after stack exit" + shardedJedis. lrange ("stringlists", 0,-1 ));
// Modify the value specified in the list:
System. out. println ("Modify the value specified in the list" + shardedJedis. lset ("stringlists", 0, "hello world "));
// Obtain the value of the modified subobject
System. out. println ("modified value" + shardedJedis. lrange ("stringlists", 0,-1 ));
// Query the length of the array of a key
System. out. println ("Get shardedJedis" + shardedJedis. llen ("stringlists "));
// Query the length of the array of a key
System. out. println ("obtain the numberlist length" + shardedJedis. llen ("numberlist "));
// When saving strings in the sorting list, you must specify the parameter as alpha. If you do not use sortingparams, you can directly use sort ("list ")
SortingParams sortingParameters = newSortingParams ();
SortingParameters. alpha ();
SortingParameters. limit (0, 3 );
// Sorted result
System. out. println ("return the sorted result stringlists" + shardedJedis. sort ("stringlists", sortingParameters ));
System. out. println ("return the sorted result numberlist" + shardedJedis. sort ("numberlist "));
// Get the substring-1 indicates the first to the last-2 indicates the second to the last
System. out. println ("the second start of the substring to the end" + shardedJedis. lrange ("stringlists", 1,-1 ));
// Obtain the second to the last
System. out. println ("getting substrings" + shardedJedis. lrange ("stringlists", 1,-2 ));
// Obtain the data with the specified subject
System. out. println ("Get the specified value of the stringlist" + shardedJedis. lindex ("stringlists", 2 ));
}
}