Package Cn.com;import Java.util.arrays;import Java.util.list;import redis.clients.jedis.jedis;import Redis.clients.jedis.jedisshardinfo;import Redis.clients.jedis.pipeline;import Redis.clients.jedis.ShardedJedis; Import Redis.clients.jedis.shardedjedispipeline;public class Redis_dispersed {/** * distributed straight along with cadence * */public static void sync _dispersed () {list<jedisshardinfo> shards = arrays.aslist (new Jedisshardinfo ("localhost", 6379), New Jedisshardinfo ("localhost", 6380)); Shardedjedis sharding = new Shardedjedis (shards); Long start = System.currenttimemillis (); for (int i = 0; i < 100000; i++) {String result = Sharding.set ("userid" + I, "age" + i); } Long end = System.currenttimemillis (); System.out.println ("Execution:" + ((End-start)/1000.0) + "seconds"); Sharding.disconnect (); }/** * Distributed Direct Connect asynchronous call * */public static void shardpipelined () {list<jedisshardinfo> shards = arrays.aslist ( New Jedisshardinfo ("LocaLhost ", 6379), new Jedisshardinfo (" localhost ", 6380)); Shardedjedis sharding = new Shardedjedis (shards); Shardedjedispipeline pipeline = sharding.pipelined (); Long start = System.currenttimemillis (); for (int i = 0; i < i++) {Pipeline.set ("userid" + I, "age" + i); } list<object> results = Pipeline.syncandreturnall (); Long end = System.currenttimemillis (); System.out.println ("Execution Time:" + ((End-start)/1000.0) + "seconds"); Sharding.disconnect (); }/** * Pipeline pipeline mode is to send some column command packages together to the server execution. * While the server will process multiple commands, the processing results of multiple commands will be packaged together and returned to the client * Note: The more commands are packaged, the more memory is consumed by the cache. So it's not as good as packing the command as much as possible * */public static void Pipeline () {Jedis Jedis = new Jedis ("localhost", 6379); Pipeline Pipeline = jedis.pipelined (); Long start = System.currenttimemillis (); for (int i = 0; i < 100000; i++) {Pipeline.set ("P" + I, "P" + i); } list<object> results = Pipeline.syncandreturnall (); Long end = System.currenttimemillis ();System.out.println ("pipelined SET:" + ((End-start)/1000.0) + "seconds"); Jedis.disconnect (); Jedis.close ();} /** * Call Transaction in pipeline * */public static void Trans_pipeline () {Jedis Jedis = new Jedis ("localhost", 6379); Long start = System.currenttimemillis (); Pipeline Pipeline = jedis.pipelined (); Pipeline.multi (); for (int i = 0; i < 100000; i++) {Pipeline.set ("" + I, "" + i); } pipeline.exec (); list<object> results = Pipeline.syncandreturnall (); Long end = System.currenttimemillis (); System.out.println ("pipelined Transaction:" + ((End-start)/1000.0) + "seconds"); Jedis.disconnect (); }public static void Main (String [] args) {shardpipelined ();}}
Redis Learning Note (9)-Pipeline/Distributed