Transaction
The notation for transactions in Jedis is to write Redis operations in a thing code block, as shown below, between multi and exec for a specific transaction.
= Jedis.multi (); T.set ("foo", "Bar"); t.exec ();
In addition, within the transaction, it is not possible to get the value through the Jedis object, but it can be obtained by transaction object, as follows:
PackageCn.edu.hdu.jedisdemo;ImportJava.util.Set;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;ImportRedis.clients.jedis.Response;Importredis.clients.jedis.Transaction; Public classApp { Public Static voidMain (string[] args) {Jedispool pool=NewJedispool (NewJedispoolconfig (), "localhost"); ///Jedis implements Closable. Hence, the Jedis instance would be//Auto-closed after the last statement. Try(Jedis Jedis =Pool.getresource ()) { ///... do stuff here ... for exampleTransaction T=Jedis.multi (); T.set ("Fool", "bar"); //jedis.get ("fool");//error, cannot use Jedis if in Multi. Transation or reset Jedis state.response<string> RESULT1 = T.get ("Fool"); T.zadd ("Foo", 1, "Barowitch"); T.zadd ("Foo", 0, "Barinsky"); T.zadd ("Foo", 0, "Barikoviev"); Response<Set<String>> sose = T.zrange ("foo", 0, 1); T.exec (); //dont forget itString Foolbar= Result1.get ();//Use Response.get () to retrieve things from a Response intSosesize = Sose.get (). Size ();//On sose.get () You can directly call Set methods! //list<object> allresults = t.exec ();//You could still get all//results at once, as beforeSystem.out.println (Foolbar); System.out.println (sosesize); } ///... when closing your application:Pool.close (); }}
Note: Theget method of the response object is called after the transaction Exec method executes , and the response object does not contain the result until the T.exec () method executes.
Pipeline
Sometimes, we need to send multiple commands, an efficient way is to use pipelines;
The pipeline can send multiple commands at once and return the results once after execution, and when each command in a set of commands does not depend on the result of the previous command, the set of commands can be sent through the pipeline together;
Pipelines can reduce the number of client-to-redis traffic and provide performance.
The following example:
Pipeline p = jedis.pipelined ();p. Set ("fool", "Bar"), P.zadd ("foo", 1, "Barowitch"); P.zadd ("foo", 0, "Barinsky"); P.zadd ("foo", 0, "Barikoviev"); Response<String> pipestring = P.get ("fool"); Response<Set<String>> sose = P.zrange ("foo", 0,-1int sosesize = sose.get (). Size (); Set<String> setback = Sose.get ();
Publish/Subscribe to subscription messages
You first create a Jedispubsub object, and then use the Jedis object to invoke the Subscribe method, which requires an incoming Jedispubsub object;
New // Listener is the Jedispubsub object, the Listener class Jedis.subscribe (Listener, "channel01");
Publish a message
Publish the message call Jedis publish method can be;
Jedis.publish (channel, message)
Note: The subscribe method is a blocking operation, and the publication and subscription cannot use the same Jedis object
A complete example
Listener Event classes:
PackageCn.edu.hdu.jedisdemo;ImportRedis.clients.jedis.JedisPubSub;classMyListenerextendsJedispubsub { Public voidonMessage (String channel, String message) {System.out.println ("Get a msg:" + "channel=" + Channel + ", message=" +message); } Public voidOnsubscribe (String Channel,intsubscribedchannels) {System.out.println ("Channel:" + Channel + ", Subscribedchannels:" +subscribedchannels); } Public voidOnunsubscribe (String Channel,intsubscribedchannels) {System.out.println ("Channel:" + Channel + ", Subscribedchannels:" +subscribedchannels); } Public voidOnpsubscribe (String pattern,intsubscribedchannels) {System.out.println ("Pattern:" + pattern + ", Subscribedchannels:" +subscribedchannels); } Public voidOnpunsubscribe (String pattern,intsubscribedchannels) {System.out.println ("Pattern:" + pattern + ", Subscribedchannels:" +subscribedchannels); } Public voidonpmessage (string pattern, string channel, String message) {System.out.println ("Pattern:" + pattern + ", Channel:" + Channel + ", Message:" +message); }}
Main method:
PackageCn.edu.hdu.jedisdemo;Importjava.util.Date;ImportJava.util.concurrent.TimeUnit;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig; Public classApp { Public Static voidMain (string[] args) {FinalJedispool pool =NewJedispool (NewJedispoolconfig (), "localhost"); //Two subscribers NewThread (NewRunnable () {@Override Public voidrun () {Jedis Jedis=Pool.getresource (); MyListener Listener=NewMyListener (); Jedis.subscribe (Listener,"Channel01"); }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {Jedis Jedis=Pool.getresource (); MyListener Listener=NewMyListener ();//Listener for Jedispubsub object, listener classJedis.subscribe (Listener, "CHANNEL01"); }}). Start (); //a publisher NewThread (NewRunnable () {@Override Public voidrun () {//Note that the Jedis object used by the subscription cannot be the same as the Jedis object used by the publication, and will be error when runJedis Jedis =Pool.getresource (); while(true){ Try{TimeUnit.SECONDS.sleep (1); } Catch(interruptedexception e) {e.printstacktrace (); } String msg=NewDate (). toLocaleString (); System.out.println ("Publish a msg:" +msg); Jedis.publish ("Channel01", MSG); }}). Start (); //pool.close (); }}
Execution Result:
CHANNEL:CHANNEL01, Subscribedchannels:1
CHANNEL:CHANNEL01, Subscribedchannels:1
Publish a msg:2016-7-20 10:03:42
Get a msg:channel=channel01, message=2016-7-20 10:03:42
Get a msg:channel=channel01, message=2016-7-20 10:03:42
Publish a msg:2016-7-20 10:03:43
Get a msg:channel=channel01, message=2016-7-20 10:03:43
Get a msg:channel=channel01, message=2016-7-20 10:03:43
Publish a msg:2016-7-20 10:03:44
Get a msg:channel=channel01, message=2016-7-20 10:03:44
Get a msg:channel=channel01, message=2016-7-20 10:03:44 distributed Shardedjedis
The Shardedjedis uses a consistent hash algorithm to determine where each key is stored, primarily for allocating server pressure.
To give a simple example:
First, open three Redis servers, using different ports (copy three copies, use different configuration files): 6379, 6380, 6381
Then, write the test code:
PackageCn.edu.hdu.jedisdemo;Importjava.util.Arrays;Importjava.util.List;Importredis.clients.jedis.Client;ImportRedis.clients.jedis.JedisPoolConfig;ImportRedis.clients.jedis.JedisShardInfo;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool; Public classApp { Public Static voidMain (string[] args) {//set up configuration for connection poolingJedispoolconfig Poolconfig =NewJedispoolconfig (); Poolconfig.setmaxtotal (2); Poolconfig.setmaxidle (1); Poolconfig.setmaxwaitmillis (2000); Poolconfig.settestonborrow (false); Poolconfig.settestonreturn (false); //set up Redis informationString host = "127.0.0.1"; Jedisshardinfo ShardInfo1=NewJedisshardinfo (host, 6379, 500); Jedisshardinfo ShardInfo2=NewJedisshardinfo (host, 6380, 500); Jedisshardinfo ShardInfo3=NewJedisshardinfo (host, 6381, 500); //Initialize ShardedjedispoolList<jedisshardinfo> infolist =arrays.aslist (ShardInfo1, ShardInfo2, ShardInfo3); Shardedjedispool Jedispool=NewShardedjedispool (Poolconfig, infolist); //some basic operations Try(Shardedjedis Jedis =Jedispool.getresource ()) {Jedis.set ("Test", "test"); String Test= Jedis.get ("Test"); SYSTEM.OUT.PRINTLN (test); } //ViewShardedjedis Jedis =Jedispool.getresource (); Jedis.set ("1111", "1111"); Jedis.set ("2222", "2222"); Jedis.set ("3333", "3333"); Jedis.set ("4444", "4444"); Client client1= Jedis.getshard ("1111"). Getclient (); Client Client2= Jedis.getshard ("2222"). Getclient (); Client Client3= Jedis.getshard ("3333"). Getclient (); Client Client4= Jedis.getshard ("4444"). Getclient (); //print key in which serverSystem.out.println ("1111 in server:" + client1.gethost () + "and port is:" +Client1.getport ()); System.out.println ("2222 in Server:" + client2.gethost () + "and port is:" +Client2.getport ()); System.out.println ("3333 in Server:" + client3.gethost () + "and port is:" +Client3.getport ()); System.out.println ("4444 in Server:" + client4.gethost () + "and port is:" +Client4.getport ());
Jedis.close (); Jedispool.close (); }}
To view the results of the output:
Test
1111 in server:127.0.0.1 and Port is:6380
2222 in server:127.0.0.1 and Port is:6379
3333 in server:127.0.0.1 and Port is:6381
4444 in server:127.0.0.1 and Port is:6381
Reference content:
Https://github.com/xetorthio/jedis/wiki/AdvancedUsage
Http://www.cnblogs.com/coder2012/p/4401153.html
http://blog.csdn.net/lihao21/article/details/48370687
Http://www.cnblogs.com/vhua/p/redis_1.html
Redis Client Development Package: Jedis Learning-Advanced applications