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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM