轉:
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】的延續,把下面的代碼增加到之前代碼后面就可以了。
- /**************************** 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<String>
- */
- public static List<String> lrange(String key, long start, long end){
- Jedis jedis = jedisPool.getResource();
- List<String> 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,標識,表示動作或者查找方向
- * <li>當count=0時,移除所有匹配的元素;</li>
- * <li>當count為負數時,移除方向是從尾到頭;</li>
- * <li>當count為正數時,移除方向是從頭到尾;</li>
- * @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
- * <li>可以為負數(-1是列表的最后一個元素,-2是列表倒數第二的元素。)</li>
- * <li>如果start大於end,則返回一個空的列表,即列表被清空</li>
- * @param end
- * <li>可以為負數(-1是列表的最后一個元素,-2是列表倒數第二的元素。)</li>
- * <li>可以超出索引,不影響結果</li>
- * @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
- * <li>當有多個key時,只要某個key值的列表有內容,即馬上返回,不再阻塞。</li>
- * <li>當所有key都沒有內容或不存在時,則會阻塞,直到有值返回或者超時。</li>
- * <li>當超期時間到達時,keys列表仍然沒有內容,則返回Null</li>
- * @return List<String>
- */
- public static List<String> blpop(int timeout, String... keys){
- Jedis jedis = jedisPool.getResource();
- List<String> values = jedis.blpop(timeout, keys);
- jedis.close();
- return values;
- }
- /**
- * 移出並獲取列表的【最后一個元素】, 如果列表沒有元素會阻塞列表直到等待超時或發現可彈出元素為止。
- * @param timeout 單位為秒
- * @param keys
- * <li>當有多個key時,只要某個key值的列表有內容,即馬上返回,不再阻塞。</li>
- * <li>當所有key都沒有內容或不存在時,則會阻塞,直到有值返回或者超時。</li>
- * <li>當超期時間到達時,keys列表仍然沒有內容,則返回Null</li>
- * @return List<String>
- */
- public static List<String> brpop(int timeout, String... keys){
- Jedis jedis = jedisPool.getResource();
- List<String> 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 列表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/