zoukankan      html  css  js  c++  java
  • Redis操作List工具类封装,Java Redis List命令封装

    转:

    Redis操作List工具类封装,Java Redis List命令封装

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    蕃薯耀 2016年9月26日 16:28:23 星期一

    http://fanshuyao.iteye.com/

    Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

    Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

    Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

    Redis操作Set工具类封装:http://fanshuyao.iteye.com/blog/2327228

    Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)

    一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

    注:下面的代码只是方法封装,缺少一部分,因为是【Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221】的延续,把下面的代码增加到之前代码后面就可以了。

    Java代码  收藏代码
    1. /**************************** redis 列表List start***************************/  
    2.       
    3.     /** 
    4.      * 将一个值插入到列表头部,value可以重复,返回列表的长度 
    5.      * @param key 
    6.      * @param value String 
    7.      * @return 返回List的长度 
    8.      */  
    9.     public static Long lpush(String key, String value){  
    10.         Jedis jedis = jedisPool.getResource();  
    11.         Long length = jedis.lpush(key, value);  
    12.         jedis.close();  
    13.         return length;  
    14.     }  
    15.       
    16.     /** 
    17.      * 将多个值插入到列表头部,value可以重复 
    18.      * @param key 
    19.      * @param values String[] 
    20.      * @return 返回List的数量size 
    21.      */  
    22.     public static Long lpush(String key, String[] values){  
    23.         Jedis jedis = jedisPool.getResource();  
    24.         Long size = jedis.lpush(key, values);  
    25.         jedis.close();  
    26.         //System.out.println(result);  
    27.         return size;  
    28.     }  
    29.       
    30.     /** 
    31.      * 获取List列表 
    32.      * @param key 
    33.      * @param start long,开始索引 
    34.      * @param end long, 结束索引 
    35.      * @return List<String> 
    36.      */  
    37.     public static List<String> lrange(String key, long start, long end){  
    38.         Jedis jedis = jedisPool.getResource();  
    39.         List<String> list = jedis.lrange(key, start, end);  
    40.         jedis.close();  
    41.         return list;  
    42.     }  
    43.       
    44.     /** 
    45.      * 通过索引获取列表中的元素 
    46.      * @param key 
    47.      * @param index,索引,0表示最新的一个元素 
    48.      * @return String 
    49.      */  
    50.     public static String lindex(String key, long index){  
    51.         Jedis jedis = jedisPool.getResource();  
    52.         String str = jedis.lindex(key, index);  
    53.         jedis.close();  
    54.         return str;  
    55.     }  
    56.       
    57.     /** 
    58.      * 获取列表长度,key为空时返回0 
    59.      * @param key 
    60.      * @return Long 
    61.      */  
    62.     public static Long llen(String key){  
    63.         Jedis jedis = jedisPool.getResource();  
    64.         Long length = jedis.llen(key);  
    65.         jedis.close();  
    66.         return length;  
    67.     }  
    68.       
    69.     /** 
    70.      * 在列表的元素前或者后插入元素,返回List的长度 
    71.      * @param key 
    72.      * @param where LIST_POSITION 
    73.      * @param pivot 以该元素作为参照物,是在它之前,还是之后(pivot:枢轴;中心点,中枢;[物]支点,支枢;[体]回转运动。) 
    74.      * @param value 
    75.      * @return Long 
    76.      */  
    77.     public static Long linsert(String key, LIST_POSITION where, String pivot, String value){  
    78.         Jedis jedis = jedisPool.getResource();  
    79.         Long length = jedis.linsert(key, where, pivot, value);  
    80.         jedis.close();  
    81.         return length;  
    82.     }  
    83.       
    84.     /** 
    85.      * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
    86.      * @param key 
    87.      * @param value String 
    88.      * @return Long 
    89.      */  
    90.     public static Long lpushx(String key, String value){  
    91.         Jedis jedis = jedisPool.getResource();  
    92.         Long length = jedis.lpushx(key, value);  
    93.         jedis.close();  
    94.         return length;  
    95.     }  
    96.       
    97.     /** 
    98.      * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
    99.      * @param key 
    100.      * @param values String[] 
    101.      * @return Long 
    102.      */  
    103.     public static Long lpushx(String key, String[] values){  
    104.         Jedis jedis = jedisPool.getResource();  
    105.         Long length = jedis.lpushx(key, values);  
    106.         jedis.close();  
    107.         return length;  
    108.     }  
    109.       
    110.     /** 
    111.      * 移除列表元素,返回移除的元素数量 
    112.      * @param key 
    113.      * @param count,标识,表示动作或者查找方向 
    114.      * <li>当count=0时,移除所有匹配的元素;</li> 
    115.      * <li>当count为负数时,移除方向是从尾到头;</li> 
    116.      * <li>当count为正数时,移除方向是从头到尾;</li> 
    117.      * @param value 匹配的元素 
    118.      * @return Long 
    119.      */  
    120.     public static Long lrem(String key, long count, String value){  
    121.         Jedis jedis = jedisPool.getResource();  
    122.         Long length = jedis.lrem(key, count, value);  
    123.         jedis.close();  
    124.         return length;  
    125.     }  
    126.       
    127.     /** 
    128.      * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true 
    129.      * @param key 
    130.      * @param index 索引 
    131.      * @param value 
    132.      * @return boolean 
    133.      */  
    134.     public static boolean lset(String key, long index, String value){  
    135.         Jedis jedis = jedisPool.getResource();  
    136.         String statusCode = jedis.lset(key, index, value);  
    137.         jedis.close();  
    138.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
    139.             return true;  
    140.         }else{  
    141.             return false;  
    142.         }  
    143.     }  
    144.       
    145.     /** 
    146.      * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 
    147.      * @param key 
    148.      * @param start 
    149.      * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
    150.      * <li>如果start大于end,则返回一个空的列表,即列表被清空</li> 
    151.      * @param end 
    152.      * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
    153.      * <li>可以超出索引,不影响结果</li> 
    154.      * @return boolean 
    155.      */  
    156.     public static boolean ltrim(String key, long start, long end){  
    157.         Jedis jedis = jedisPool.getResource();  
    158.         String statusCode = jedis.ltrim(key, start, end);  
    159.         jedis.close();  
    160.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
    161.             return true;  
    162.         }else{  
    163.             return false;  
    164.         }  
    165.     }  
    166.       
    167.     /** 
    168.      * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null 
    169.      * @param key 
    170.      * @return String 
    171.      */  
    172.     public static String lpop(String key){  
    173.         Jedis jedis = jedisPool.getResource();  
    174.         String value = jedis.lpop(key);  
    175.         jedis.close();  
    176.         return value;  
    177.     }  
    178.       
    179.     /** 
    180.      * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null 
    181.      * @param key 
    182.      * @return String 
    183.      */  
    184.     public static String rpop(String key){  
    185.         Jedis jedis = jedisPool.getResource();  
    186.         String value = jedis.rpop(key);  
    187.         jedis.close();  
    188.         return value;  
    189.     }  
    190.       
    191.     /** 
    192.      * 在列表中的尾部添加一个个值,返回列表的长度 
    193.      * @param key 
    194.      * @param value 
    195.      * @return Long 
    196.      */  
    197.     public static Long rpush(String key, String value){  
    198.         Jedis jedis = jedisPool.getResource();  
    199.         Long length = jedis.rpush(key, value);  
    200.         jedis.close();  
    201.         return length;  
    202.     }  
    203.       
    204.     /** 
    205.      * 在列表中的尾部添加多个值,返回列表的长度 
    206.      * @param key 
    207.      * @param values 
    208.      * @return Long 
    209.      */  
    210.     public static Long rpush(String key, String[] values){  
    211.         Jedis jedis = jedisPool.getResource();  
    212.         Long length = jedis.rpush(key, values);  
    213.         jedis.close();  
    214.         return length;  
    215.     }  
    216.       
    217.     /** 
    218.      * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度 
    219.      * @param key 
    220.      * @param value 
    221.      * @return Long 
    222.      */  
    223.     public static Long rpushx(String key, String value){  
    224.         Jedis jedis = jedisPool.getResource();  
    225.         Long length = jedis.rpushx(key, value);  
    226.         jedis.close();  
    227.         return length;  
    228.     }  
    229.       
    230.     /** 
    231.      * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回 
    232.      * @param sourceKey 源列表的key,当源key不存在时,结果返回Null 
    233.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
    234.      * @return String 
    235.      */  
    236.     public static String rpopLpush(String sourceKey, String targetKey){  
    237.         Jedis jedis = jedisPool.getResource();  
    238.         String value = jedis.rpoplpush(sourceKey, targetKey);  
    239.         jedis.close();  
    240.         return value;  
    241.     }  
    242.       
    243.     /** 
    244.      * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
    245.      * @param timeout 单位为秒 
    246.      * @param keys 
    247.      * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
    248.      * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
    249.      * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
    250.      * @return List<String> 
    251.      */  
    252.     public static List<String> blpop(int timeout, String... keys){  
    253.         Jedis jedis = jedisPool.getResource();  
    254.         List<String> values = jedis.blpop(timeout, keys);  
    255.         jedis.close();  
    256.         return values;  
    257.     }  
    258.       
    259.     /** 
    260.      * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
    261.      * @param timeout 单位为秒 
    262.      * @param keys 
    263.      * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
    264.      * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
    265.      * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
    266.      * @return List<String> 
    267.      */  
    268.     public static List<String> brpop(int timeout, String... keys){  
    269.         Jedis jedis = jedisPool.getResource();  
    270.         List<String> values = jedis.brpop(timeout, keys);  
    271.         jedis.close();  
    272.         return values;  
    273.     }  
    274.       
    275.     /** 
    276.      * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;  
    277.      * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
    278.      * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞 
    279.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
    280.      * @param timeout 单位为秒 
    281.      * @return String 
    282.      */  
    283.     public static String brpopLpush(String sourceKey, String targetKey, int timeout){  
    284.         Jedis jedis = jedisPool.getResource();  
    285.         String value = jedis.brpoplpush(sourceKey, targetKey, timeout);  
    286.         jedis.close();  
    287.         return value;  
    288.     }  
    289.       
    290.     /**************************** redis 列表List end***************************/  

     Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

    Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

    Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    蕃薯耀 2016年9月26日 16:28:23 星期一

    http://fanshuyao.iteye.com/

  • 相关阅读:
    fabrci网络调优
    fabric链码容器
    fabric文档查阅
    fabric基础设施管理-(五)移除网络
    fabric源码编译
    fabric网络状态监控
    fabric基础设施管理-(六)配置管理
    Scheme宏基础入门(转载)
    GO语言程序中解决中文日期格式的解析问题
    临别之际
  • 原文地址:https://www.cnblogs.com/libin6505/p/10695374.html
Copyright © 2011-2022 走看看