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

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

 


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 

  36.      */  

  37.     public static List lrange(String key, long start, long end){  

  38.         Jedis jedis = jedisPool.getResource();  

  39.         List 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.      * 

  115. 当count=0时,移除所有匹配的元素;
  116.  

  117.      * 

  118. 当count为负数时,移除方向是从尾到头;
  119.  

  120.      * 

  121. 当count为正数时,移除方向是从头到尾;
  122.  

  123.      * @param value 匹配的元素 

  124.      * @return Long 

  125.      */  

  126.     public static Long lrem(String key, long count, String value){  

  127.         Jedis jedis = jedisPool.getResource();  

  128.         Long length = jedis.lrem(key, count, value);  

  129.         jedis.close();  

  130.         return length;  

  131.     }  

  132.       

  133.     /** 

  134.      * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true 

  135.      * @param key 

  136.      * @param index 索引 

  137.      * @param value 

  138.      * @return boolean 

  139.      */  

  140.     public static boolean lset(String key, long index, String value){  

  141.         Jedis jedis = jedisPool.getResource();  

  142.         String statusCode = jedis.lset(key, index, value);  

  143.         jedis.close();  

  144.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  

  145.             return true;  

  146.         }else{  

  147.             return false;  

  148.         }  

  149.     }  

  150.       

  151.     /** 

  152.      * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 

  153.      * @param key 

  154.      * @param start 

  155.      * 

  156. 可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)
  157.  

  158.      * 

  159. 如果start大于end,则返回一个空的列表,即列表被清空
  160.  

  161.      * @param end 

  162.      * 

  163. 可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)
  164.  

  165.      * 

  166. 可以超出索引,不影响结果
  167.  

  168.      * @return boolean 

  169.      */  

  170.     public static boolean ltrim(String key, long start, long end){  

  171.         Jedis jedis = jedisPool.getResource();  

  172.         String statusCode = jedis.ltrim(key, start, end);  

  173.         jedis.close();  

  174.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  

  175.             return true;  

  176.         }else{  

  177.             return false;  

  178.         }  

  179.     }  

  180.       

  181.     /** 

  182.      * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null 

  183.      * @param key 

  184.      * @return String 

  185.      */  

  186.     public static String lpop(String key){  

  187.         Jedis jedis = jedisPool.getResource();  

  188.         String value = jedis.lpop(key);  

  189.         jedis.close();  

  190.         return value;  

  191.     }  

  192.       

  193.     /** 

  194.      * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null 

  195.      * @param key 

  196.      * @return String 

  197.      */  

  198.     public static String rpop(String key){  

  199.         Jedis jedis = jedisPool.getResource();  

  200.         String value = jedis.rpop(key);  

  201.         jedis.close();  

  202.         return value;  

  203.     }  

  204.       

  205.     /** 

  206.      * 在列表中的尾部添加一个个值,返回列表的长度 

  207.      * @param key 

  208.      * @param value 

  209.      * @return Long 

  210.      */  

  211.     public static Long rpush(String key, String value){  

  212.         Jedis jedis = jedisPool.getResource();  

  213.         Long length = jedis.rpush(key, value);  

  214.         jedis.close();  

  215.         return length;  

  216.     }  

  217.       

  218.     /** 

  219.      * 在列表中的尾部添加多个值,返回列表的长度 

  220.      * @param key 

  221.      * @param values 

  222.      * @return Long 

  223.      */  

  224.     public static Long rpush(String key, String[] values){  

  225.         Jedis jedis = jedisPool.getResource();  

  226.         Long length = jedis.rpush(key, values);  

  227.         jedis.close();  

  228.         return length;  

  229.     }  

  230.       

  231.     /** 

  232.      * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度 

  233.      * @param key 

  234.      * @param value 

  235.      * @return Long 

  236.      */  

  237.     public static Long rpushx(String key, String value){  

  238.         Jedis jedis = jedisPool.getResource();  

  239.         Long length = jedis.rpushx(key, value);  

  240.         jedis.close();  

  241.         return length;  

  242.     }  

  243.       

  244.     /** 

  245.      * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回 

  246.      * @param sourceKey 源列表的key,当源key不存在时,结果返回Null 

  247.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 

  248.      * @return String 

  249.      */  

  250.     public static String rpopLpush(String sourceKey, String targetKey){  

  251.         Jedis jedis = jedisPool.getResource();  

  252.         String value = jedis.rpoplpush(sourceKey, targetKey);  

  253.         jedis.close();  

  254.         return value;  

  255.     }  

  256.       

  257.     /** 

  258.      * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 

  259.      * @param timeout 单位为秒 

  260.      * @param keys 

  261.      * 

  262. 当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。
  263.  

  264.      * 

  265. 当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。
  266.  

  267.      * 

  268. 当超期时间到达时,keys列表仍然没有内容,则返回Null
  269.  

  270.      * @return List 

  271.      */  

  272.     public static List blpop(int timeout, String... keys){  

  273.         Jedis jedis = jedisPool.getResource();  

  274.         List values = jedis.blpop(timeout, keys);  

  275.         jedis.close();  

  276.         return values;  

  277.     }  

  278.       

  279.     /** 

  280.      * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 

  281.      * @param timeout 单位为秒 

  282.      * @param keys 

  283.      * 

  284. 当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。
  285.  

  286.      * 

  287. 当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。
  288.  

  289.      * 

  290. 当超期时间到达时,keys列表仍然没有内容,则返回Null
  291.  

  292.      * @return List 

  293.      */  

  294.     public static List brpop(int timeout, String... keys){  

  295.         Jedis jedis = jedisPool.getResource();  

  296.         List values = jedis.brpop(timeout, keys);  

  297.         jedis.close();  

  298.         return values;  

  299.     }  

  300.       

  301.     /** 

  302.      * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;  

  303.      * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 

  304.      * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞 

  305.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 

  306.      * @param timeout 单位为秒 

  307.      * @return String 

  308.      */  

  309.     public static String brpopLpush(String sourceKey, String targetKey, int timeout){  

  310.         Jedis jedis = jedisPool.getResource();  

  311.         String value = jedis.brpoplpush(sourceKey, targetKey, timeout);  

  312.         jedis.close();  

  313.         return value;  

  314.     }  

下载  

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

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