Jedis is a well-known key-value storage System, and as its official recommended Java version of the client Jedis is also very strong and stable, supporting transactions, pipelines and the implementation of Jedis itself is distributed.
Here is a brief introduction and comparison of the Jedis about transactions, pipelines, and distributed invocation methods:
One, common synchronization mode
The simplest and most basic way to call,
@Testpublic void Test1normal () {Jedis Jedis = new Jedis ("localhost"); Long start = System.currenttimemillis (); for (int i = 0; i < 100000; i++) {String result = Jedis.set ("n" + I, "n" + i); } Long end = System.currenttimemillis (); System.out.println ("Simple SET:" + ((End-start)/1000.0) + "seconds"); Jedis.disconnect ();}
It's simple, set
you can return the result after each time, and the token is successful.
Ii. mode of affairs (transactions)
Redis's transaction is simple, and his primary purpose is to ensure that commands in a client-initiated transaction can be executed consecutively, without inserting other client commands.
Look at the following example:
@Testpublic void Test2trans () {Jedis Jedis = new Jedis ("localhost"); Long start = System.currenttimemillis (); Transaction tx = Jedis.multi (); for (int i = 0; i < 100000; i++) {Tx.set ("T" + I, "T" + i); } list<object> results = Tx.exec (); Long end = System.currenttimemillis (); System.out.println ("Transaction SET:" + ((End-start)/1000.0) + "seconds"); Jedis.disconnect ();}
We call the jedis.watch(…)
method to monitor the key, and if the key value changes after the call, the entire transaction fails. Additionally, an operation in the transaction fails, and no other operations are rolled back. This is a point to note. Also, we can use discard()
methods to cancel transactions.
Third, Pipeline (pipelining)
Sometimes we need to send multiple instructions at once, asynchronously, and wait for the results to be returned. This can achieve very good execution efficiency. This is the pipeline, called by the following method:
@Testpublic void test3pipelined () {Jedis Jedis = new Jedis ("localhost"); 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 ();}
Iv. invoking a transaction in a pipeline
In terms of the methods provided by Jedis, it is possible to use transactions in the pipeline with the following code:
@Testpublic void test4combpipelinetrans () { jedis = new Jedis ("localhost"); 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 ();}
But after testing (see later in this article), it was found that its efficiency was similar to that of a single-use transaction, and even slightly nearly.
Five, distributed direct connection synchronous call
@Testpublic void test5shardnormal () { 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 ("sn" + i, "n" + i); } long end = system.currenttimemillis (); system.out.println ("[email protected] set: " + (end - Start)/1000.0) + " seconds"); sharding.disconnect ();}
This is a distributed direct connection, and is a synchronous call, with each step returning execution results. Similarly, there are asynchronous pipeline calls.
Vi. distributed, direct-attached asynchronous invocation
@Testpublic void test6shardpipelined () { 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 < 100000; i++) { pipeline.set ("SP" + i, "P" + i); } List<Object> results = Pipeline.syncandreturnall (); long&nbSp;end = system.currenttimemillis (); system.out.println ("[email protected] set: " + ((End - start)/1000.0) + " seconds "); sharding.disconnect ();}
Vii. Distributed Connection Pool synchronous invocation
If your distributed calling code is running in the thread, then the above two direct connect calls are inappropriate because the direct connection is non-thread-safe, and at this point you will have to select the Connect pool call.
@Testpublic void test7shardsimplepool () { List<JedisShardInfo> shards = arrays.aslist ( new jedisshardinfo ("localhost", 6379), new jedisshardinfo ("localhost", 6380)); shardedjedispool pool = new shardedjedispool (New jedispoolconfig (), shards); Shardedjedis one = pool.getresource (); long start = System.currenttimemillis (); for (int i = 0; i < 100000; i++) { string result = one.set ("SPN" + i, "n" + i); } long end = system.currenttimemillis (); pool.returnresource (one); system.out.println ("[email protected] set: " + ((End - start)/1000.0) + " seconds "); pool.destroy ();}
The above is synchronous, and of course there are asynchronous methods.
Viii. Distributed Connection Pool asynchronous invocation
@Testpublic void test8shardpipelinedpool () { List<JedisShardInfo> shards = arrays.aslist ( new jedisshardinfo ("localhost", 6379), new jedisshardinfo ("localhost", 6380)); shardedjedispool pool = new shardedjedispool (New jedispoolconfig (), shards); Shardedjedis one = pool.getresource (); shardedjedispipeline pipeline = one.pipelined (); long start = System.currenttimemillis (); for (int i = 0; i < 100000; i++) { pipeline.set ("Sppn" + i, "n" + i); } list<object> results = pipeline.syncandreturnall (); Long end = system.currenttimemillis (); pool.returnresource (one); system.out.println ("[email protected] set: " + (end - Start)/1000.0) + " seconds"); pool.destroy ();}
Using Redis in Java (reproduced)