jedis调用redis之总结

package com.alex.xiyoubang;

import java.util.Arrays;
import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.Transaction;


public class JedisCompare {

    private static Jedis jedis;
    private static ShardedJedis sharding;
    private static ShardedJedisPool pool;

    public static void main(String[] args) {
        normal();
        transaction();
        pipelined();
        combPipelineTrans();
        shardNormal();
        shardpipelined();
        shardSimplePool();
        shardPipelinedPool();
    }
    /** * 初始化 */
    public static void initJedis() {
        List<JedisShardInfo> shards = Arrays.asList(
                new JedisShardInfo("192.168.1.171",6379),
                new JedisShardInfo("192.168.1.171",6379)); //使用相同的ip:port,仅作测试


        jedis = new Jedis("192.168.1.171"); 
        sharding = new ShardedJedis(shards);

        pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
    }
    /** * 销毁 */
    public static void destroyJedis() {
        jedis.disconnect();
        sharding.disconnect();
        pool.destroy();
    }

    /** * 最简单和基础的调用方式 */
    public static void normal() {
        initJedis();
        System.out.println(jedis.flushDB());
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String result = jedis.set("normal" + i, "normal" + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("normal waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 事务方式 */
    public static void transaction() {
        initJedis();
        long start = System.currentTimeMillis();
        Transaction tx = jedis.multi();
        for (int i = 0; i < 1000; i++) {
            tx.set("transaction" + i, "transaction" + i);
        }
        List<Object> results = tx.exec();
        long end = System.currentTimeMillis();
        System.out.println("transaction waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 管道 */
    public static void pipelined() {
        initJedis();
        Pipeline pipeline = jedis.pipelined();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            pipeline.set("pipelined" + i, "pipelined" + i);
        }
        List<Object> results = pipeline.syncAndReturnAll();
        long end = System.currentTimeMillis();
        System.out.println("pipelined waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 管道中调用事务 */
    public static void combPipelineTrans() {
        initJedis();
        long start = System.currentTimeMillis();
        Pipeline pipeline = jedis.pipelined();
        pipeline.multi();
        for (int i = 0; i < 1000; i++) {
            pipeline.set("combPipelineTrans" + i, "combPipelineTrans" + i);
        }
        pipeline.exec();
        List<Object> results = pipeline.syncAndReturnAll();
        long end = System.currentTimeMillis();
        System.out.println("combPipelineTrans waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 分布式直连同步调用 */
    public static void shardNormal() {
        initJedis();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String result = sharding.set("shardNormal" + i, "shardNormal" + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("shardNormal waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 分布式直连异步调用 */
    public static void shardpipelined() {
        initJedis();
        ShardedJedisPipeline pipeline = sharding.pipelined();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            pipeline.set("shardpipelined" + i, "shardpipelined" + i);
        }
        List<Object> results = pipeline.syncAndReturnAll();
        long end = System.currentTimeMillis();
        System.out.println("shardpipelined waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 分布式连接池同步调用 */
    public static void shardSimplePool() {
        initJedis();
        ShardedJedis one = pool.getResource();

        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String result = one.set("shardSimplePool" + i, "shardSimplePool" + i);
        }
        long end = System.currentTimeMillis();
        pool.returnResource(one);
        System.out.println("shardSimplePool waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }

   /** * 分布式连接池异步调用 */
    public static void shardPipelinedPool() {
        initJedis();
        ShardedJedis one = pool.getResource();

        ShardedJedisPipeline pipeline = one.pipelined();

        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            pipeline.set("shardPipelinedPool" + i, "shardPipelinedPool" + i);
        }
        List<Object> results = pipeline.syncAndReturnAll();
        long end = System.currentTimeMillis();
        pool.returnResource(one);
        System.out.println("shardPipelinedPool waste: " + ((end - start)/1000.0) + " seconds");
        destroyJedis();
    }
}

结果:jedis调用redis之总结_第1张图片

1、事务和管道都是异步模式。在事务和管道中不能同步查询结果。
2、事务和管道都是异步的,在管道中再进行事务调用,没有必要,不如直接进行事务模式。
3、分布式中,连接池的性能比直连的性能略好。
4、分布式调用中不支持事务。
因为事务是在服务器端实现,而在分布式中,每批次的调用对象都可能访问不同的机器,所以,没法进行事务。
5、分布式中,连接池方式调用不但线程安全外,根据上面的测试数据,也可以看出连接池比直连的效率更好。

你可能感兴趣的:(redis,总结,jedis,分布式,比较)