• 【转载】Jedis对管道、事务以及Watch的操作详细解析


    转载地址:http://blog.csdn.net/liyantianmin/article/details/51613772

    1、Pipeline

    利用pipeline的方式从client打包多条命令一起发出,不需要等待单条命令的响应返回,而Redis服务端会处理完多条命令后会将多条命令的处理结果打包到一起返回给客户端。所以pipeline适合批处理作业可以提升效率如:

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. public static void testMget() {  
    2.         Jedis jedis = RedisCacheClient.getInstrance().getClient();  
    3.         Set<String> keys = jedis.keys("cvfeedBackHandl_*");  
    4.         List<String> result = Lists.newArrayList();  
    5.         long t1 = System.currentTimeMillis();  
    6.         for (String key : keys) {  
    7.             result.add(jedis.get(key));  
    8.         }  
    9.         for (String src : result) {  
    10.             System.out.println(src);  
    11.         }  
    12.         System.out.println(System.currentTimeMillis() - t1);  
    13.     }  
    14.   
    15.     public static void testPipline() {  
    16.         Jedis jedis = RedisCacheClient.getInstrance().getClient();  
    17.         Set<String> keys = jedis.keys("cvfeedBackHandl_*");  
    18.         List<Object> result = Lists.newArrayList();  
    19.         Pipeline pipelined = jedis.pipelined();  
    20.         long t1 = System.currentTimeMillis();  
    21.         for (String key : keys) {  
    22.             pipelined.<span style="font-family: Arial;">get</span>("testabcd");  
    23.         }  
    24.         result = pipelined.syncAndReturnAll();  
    25.         for (Object src : result) {  
    26.             System.out.println(src);  
    27.         }  
    28.         System.out.println(System.currentTimeMillis() - t1);  
    29.     }  

    如第一个方法执行的时间是82ms

    第二个方法执行的时间是9ms

    注意:pipeline和事务都是异步调用返回结果的,即并不是等待每条命令执行完立马返回结果而是等待所有命令执行完之后再返回结果。pipelined.syncAndReturnAll()返回的是参与打包执行的每条命令的结果。如果上面改成:

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. for (String key : keys) {//keys长度为5  
    2.             pipelined.get(key);  
    3.             pipelined.del("testabcd");  
    4.  }  

    返回结果将是

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. "test1"  
    2. 1  
    3. "test2"  
    4. 0  
    5. "test2"  
    6. 0  
    7. "test4"  
    8. 0  
    9. "test5"  
    10. 0  


    2、事务

      事务是保证事务内的所有命令是原子操作,一般配合watch使用,事务的执行结果和pipeline一样都是采用异步的方式获取结果,multi.exec()提交事务,如果执行成功,其返回的结果和pipeline一样是所有命令的返回值,如果事务里面有两个命令那么事务的exec返回值会把两个命令的返回值组合在一起返回。如果事务被取消返回null。

    3、watch

    一般是和事务一起使用,当对某个key进行watch后如果其他的客户端对这个key进行了更改,那么本次事务会被取消,事务的exec会返回null。jedis.watch(key)都会返回OK
    eg:

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. public static void testWach(){  
    2.        Jedis jedis = RedisCacheClient.getInstrance().getClient();  
    3.        String watch = jedis.watch("testabcd");  
    4.        System.out.println(Thread.currentThread().getName()+"--"+watch);  
    5.        Transaction multi = jedis.multi();  
    6.        multi.set("testabcd", "23432");  
    7.        try {  
    8.            Thread.sleep(3000);  
    9.        } catch (InterruptedException e) {  
    10.            e.printStackTrace();  
    11.        }  
    12.        List<Object> exec = multi.exec();  
    13.        System.out.println("---"+exec);  
    14.        jedis.unwatch();  
    15.    }  
    16.    public static void testWatch2(){  
    17.        Jedis jedis = RedisCacheClient.getInstrance().getClient();  
    18.        String watch = jedis.watch("testabcd2");  
    19.        System.out.println(Thread.currentThread().getName()+"--"+watch);  
    20.        Transaction multi = jedis.multi();  
    21.        multi.set("testabcd", "125");  
    22.        List<Object> exec = multi.exec();  
    23.        System.out.println("--->>"+exec);  
    24.    }  


    Thread-2--OK
    Thread-0--OK
    --->>[OK]

    ---null//事务取消

    4、事务与管道

         当对某个key进行watch时,如果其他的客户端对key进行了更改事务可以做到取消事务操作但是管道不可以

  • 相关阅读:
    FLV视频转换的利器 ffmpeg.exe
    ffmpeg参数设定解说
    SQL里加减日期
    SQL Server 获得影响行数
    CheckBoxList RadioButtonList 不生成table 表示
    SQL语句 从一个表读取数据,写入到另一个表的相同字段中
    ffmpeg和Mencoder使用实例小全
    执行存储过程出现:"不是有效的标识符。"
    SQL 格式化超长的字段
    js遍历选中的dom元素
  • 原文地址:https://www.cnblogs.com/xujishou/p/6420235.html
Copyright © 2020-2023  润新知