获取list缓存的内容
/**
* lRange命令 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1 代表所有值
* @return 获取到的缓存内容
*/
public List<Object> lRange(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取list缓存的长度
/**
* lLen命令 获取list缓存的长度
*
* @param key 键
* @return 获取到的长度
*/
public long lLen(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
从list的右侧插入值
/**
* rPush 从list的右侧插入值
*
* @param key 键
* @param value 值
* @return 436
*/
public boolean rPush(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的右侧插入多个值
/**
* rPush 从list的右侧插入多个值
*
* @param key 键
* @param value 多个值
* @return 取出是否成功
*/
public boolean rPush(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的右侧插入多个值,并设置超时时间
/**
* rPush 从list的右侧插入多个值,并设置超时时间
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return 操作是否成功
*/
public boolean rPush(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的左侧插入值
/**
* lPush 从list的左侧插入值
*
* @param key 键
* @param value 值
* @return 436
*/
public boolean lPush(String key, Object value) {
try {
redisTemplate.opsForList().leftPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的左侧插入值并设置超时时间
/**
* rPush 从list的左侧插入值并设置超时时间
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return 操作是否成功
*/
public boolean lPush(String key, Object value, long time) {
try {
redisTemplate.opsForList().leftPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的左侧插入多个值
/**
* rPush 从list的左侧插入多个值
*
* @param key 键
* @param value 多个值
* @return 操作是否成功
*/
public boolean lPushAll(String key, List<Object> value) {
try {
redisTemplate.opsForList().leftPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从list的左侧插入多个值,并设置超时时间
/**
* lPush 从list的左侧插入多个值,并设置超时时间
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return 操作是否成功
*/
public boolean lPush(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().leftPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从左侧弹出一个元素(非阻塞,存在线程安全)
/**
* lPop命令 从左侧弹出一个元素(非阻塞,存在线程安全)
*
* @param key 键
* @return 弹出的元素
*/
public Object lPop(String key) {
try {
redisTemplate.opsForList().leftPop(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从左侧弹出一个元素(阻塞,设置有阻塞时间)
/**
* bLeftPop 从左侧弹出一个元素(阻塞,设置有阻塞时间)
*
* @param key 键
* @param timeout 阻塞时间
* @return 弹出的数据
*/
public Object bLeftPop(String key, long timeout) {
try {
redisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从右侧弹出一个元素(非阻塞,存在线程安全)
/**
* rPop命令 从右侧弹出一个元素(非阻塞,存在线程安全)
*
* @param key 键
* @return 弹出的元素
*/
public Object rPop(String key) {
try {
redisTemplate.opsForList().rightPop(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
从右侧弹出一个元素(阻塞,设置有阻塞时间)
/**
* bRightPop命令 从右侧弹出一个元素(阻塞,设置有阻塞时间)
*
* @param key 键
* @param timeout 阻塞时间
* @return 弹出的元素
*/
public Object bRightPop(String key, long timeout) {
try {
redisTemplate.opsForList().rightPop(key, timeout, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
通过索引 获取list中的值
/**
* lIndex命令 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return 获取到的数据
*/
public Object lIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
根据索引修改list中的某条数据
/**
* lSet命令 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
* @return 509
*/
public boolean lSet(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
移除N个值的value
/**
* lRem 移除N个值的value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public long lRem(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
移除 start到end的值
/**
* ltrim 命令 移除 start到end的值
*
* @param key 键
* @param start 起始位置
* @param end 结束位置
* @return 是否移除成功
*/
public boolean ltrim(String key, long start, long end) {
try {
redisTemplate.opsForList().trim(key, start, end);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从fromKey右侧取出,由左侧插入destKey中(非阻塞)
/**
* rightPopLeftPush 从fromKey右侧取出,由左侧插入destKey中(非阻塞)
*
* @param fromKey 来源键
* @param destKey 目标键
* @return 是否成功
*/
public boolean rightPopLeftPush(String fromKey, String destKey) {
try {
redisTemplate.opsForList().rightPopAndLeftPush(fromKey, destKey);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
从fromKey右侧取出,destKey(阻塞,设有阻塞时间)
/**
* rightPopAndLeftPush 从fromKey右侧取出,destKey(阻塞,设有阻塞时间)
*
* @param fromKey 来源键
* @param destKey 目标键
* @return 是否取出成功
*/
public boolean bRightPopLeftPush(String fromKey, String destKey, long timeout) {
try {
redisTemplate.opsForList().rightPopAndLeftPush(fromKey, destKey, timeout, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}