设为首页 收藏本站
查看: 890|回复: 0

[经验分享] Redis操作List工具类封装,Java Redis List命令封装

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2018-11-4 13:28:37 | 显示全部楼层 |阅读模式
  Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)
  一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。
  Java代码  下载

  •   /**************************** redis 列表List start***************************/

  •   /**
  •   * 将一个值插入到列表头部,value可以重复,返回列表的长度
  •   * @param key
  •   * @param value String
  •   * @return 返回List的长度
  •   */
  •   public static Long lpush(String key, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.lpush(key, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 将多个值插入到列表头部,value可以重复
  •   * @param key
  •   * @param values String[]
  •   * @return 返回List的数量size
  •   */
  •   public static Long lpush(String key, String[] values){
  •   Jedis jedis = jedisPool.getResource();
  •   Long size = jedis.lpush(key, values);
  •   jedis.close();
  •   //System.out.println(result);
  •   return size;
  •   }

  •   /**
  •   * 获取List列表
  •   * @param key
  •   * @param start long,开始索引
  •   * @param end long, 结束索引
  •   * @return List
  •   */
  •   public static List lrange(String key, long start, long end){
  •   Jedis jedis = jedisPool.getResource();
  •   List list = jedis.lrange(key, start, end);
  •   jedis.close();
  •   return list;
  •   }

  •   /**
  •   * 通过索引获取列表中的元素
  •   * @param key
  •   * @param index,索引,0表示最新的一个元素
  •   * @return String
  •   */
  •   public static String lindex(String key, long index){
  •   Jedis jedis = jedisPool.getResource();
  •   String str = jedis.lindex(key, index);
  •   jedis.close();
  •   return str;
  •   }

  •   /**
  •   * 获取列表长度,key为空时返回0
  •   * @param key
  •   * @return Long
  •   */
  •   public static Long llen(String key){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.llen(key);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 在列表的元素前或者后插入元素,返回List的长度
  •   * @param key
  •   * @param where LIST_POSITION
  •   * @param pivot 以该元素作为参照物,是在它之前,还是之后(pivot:枢轴;中心点,中枢;[物]支点,支枢;[体]回转运动。)
  •   * @param value
  •   * @return Long
  •   */
  •   public static Long linsert(String key, LIST_POSITION where, String pivot, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.linsert(key, where, pivot, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0)
  •   * @param key
  •   * @param value String
  •   * @return Long
  •   */
  •   public static Long lpushx(String key, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.lpushx(key, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0)
  •   * @param key
  •   * @param values String[]
  •   * @return Long
  •   */
  •   public static Long lpushx(String key, String[] values){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.lpushx(key, values);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 移除列表元素,返回移除的元素数量
  •   * @param key
  •   * @param count,标识,表示动作或者查找方向
  •   * 当count=0时,移除所有匹配的元素;
  •   * 当count为负数时,移除方向是从尾到头;
  •   * 当count为正数时,移除方向是从头到尾;
  •   * @param value 匹配的元素
  •   * @return Long
  •   */
  •   public static Long lrem(String key, long count, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.lrem(key, count, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true
  •   * @param key
  •   * @param index 索引
  •   * @param value
  •   * @return boolean
  •   */
  •   public static boolean lset(String key, long index, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   String statusCode = jedis.lset(key, index, value);
  •   jedis.close();
  •   if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  •   return true;
  •   }else{
  •   return false;
  •   }
  •   }

  •   /**
  •   * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
  •   * @param key
  •   * @param start
  •   * 可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)
  •   * 如果start大于end,则返回一个空的列表,即列表被清空
  •   * @param end
  •   * 可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)
  •   * 可以超出索引,不影响结果
  •   * @return boolean
  •   */
  •   public static boolean ltrim(String key, long start, long end){
  •   Jedis jedis = jedisPool.getResource();
  •   String statusCode = jedis.ltrim(key, start, end);
  •   jedis.close();
  •   if(SUCCESS_OK.equalsIgnoreCase(statusCode)){
  •   return true;
  •   }else{
  •   return false;
  •   }
  •   }

  •   /**
  •   * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null
  •   * @param key
  •   * @return String
  •   */
  •   public static String lpop(String key){
  •   Jedis jedis = jedisPool.getResource();
  •   String value = jedis.lpop(key);
  •   jedis.close();
  •   return value;
  •   }

  •   /**
  •   * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null
  •   * @param key
  •   * @return String
  •   */
  •   public static String rpop(String key){
  •   Jedis jedis = jedisPool.getResource();
  •   String value = jedis.rpop(key);
  •   jedis.close();
  •   return value;
  •   }

  •   /**
  •   * 在列表中的尾部添加一个个值,返回列表的长度
  •   * @param key
  •   * @param value
  •   * @return Long
  •   */
  •   public static Long rpush(String key, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.rpush(key, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 在列表中的尾部添加多个值,返回列表的长度
  •   * @param key
  •   * @param values
  •   * @return Long
  •   */
  •   public static Long rpush(String key, String[] values){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.rpush(key, values);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度
  •   * @param key
  •   * @param value
  •   * @return Long
  •   */
  •   public static Long rpushx(String key, String value){
  •   Jedis jedis = jedisPool.getResource();
  •   Long length = jedis.rpushx(key, value);
  •   jedis.close();
  •   return length;
  •   }

  •   /**
  •   * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
  •   * @param sourceKey 源列表的key,当源key不存在时,结果返回Null
  •   * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的
  •   * @return String
  •   */
  •   public static String rpopLpush(String sourceKey, String targetKey){
  •   Jedis jedis = jedisPool.getResource();
  •   String value = jedis.rpoplpush(sourceKey, targetKey);
  •   jedis.close();
  •   return value;
  •   }

  •   /**
  •   * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  •   * @param timeout 单位为秒
  •   * @param keys
  •   * 当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。
  •   * 当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。
  •   * 当超期时间到达时,keys列表仍然没有内容,则返回Null
  •   * @return List
  •   */
  •   public static List blpop(int timeout, String... keys){
  •   Jedis jedis = jedisPool.getResource();
  •   List values = jedis.blpop(timeout, keys);
  •   jedis.close();
  •   return values;
  •   }

  •   /**
  •   * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  •   * @param timeout 单位为秒
  •   * @param keys
  •   * 当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。
  •   * 当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。
  •   * 当超期时间到达时,keys列表仍然没有内容,则返回Null
  •   * @return List
  •   */
  •   public static List brpop(int timeout, String... keys){
  •   Jedis jedis = jedisPool.getResource();
  •   List values = jedis.brpop(timeout, keys);
  •   jedis.close();
  •   return values;
  •   }

  •   /**
  •   * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;
  •   * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
  •   * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞
  •   * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的
  •   * @param timeout 单位为秒
  •   * @return String
  •   */
  •   public static String brpopLpush(String sourceKey, String targetKey, int timeout){
  •   Jedis jedis = jedisPool.getResource();
  •   String value = jedis.brpoplpush(sourceKey, targetKey, timeout);
  •   jedis.close();
  •   return value;
  •   }
  下载
  Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素导列表的头部(左边)或者尾部(右边)
  一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-630706-1-1.html 上篇帖子: Redis Sorted Set有序集合 存储操作方法 下篇帖子: Redis 常用命令之-----键值命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表