redisTemplate使用
持续更新中…
操作ZSet
基本操作
ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
//添加
zSetOperations.add(key,value,score);
// 获取
// 获取ZSet大小
zSetOperations.zCard(key);
//按score从小到大排列的获取,index为0开始,先获取score小的,
zSetOperations.range(key,startIndex,endIndex);
//根据score获取,并且指定下标offset、数目count,
zSetOperations.rangeByScore(key,startScore,endScore,offset,count);
//按score从大到小排列的获取,index为0开始,先获取score大的,range有的获取方法,reversRange都有
zSetOperations.reverseRange(key,startIndex,endIndex);
// 移除
zSetOperations.remove(key,value);
zSetOperations.removeRange(key,startIndex,endIndex);
zSetOperations.removeRangeByScore(key,startScore,endScore);
// 更新
zSetOperations.incrementScore(key,value,score);
其他操作
判断设置Zset集合中存在该value
/**
* 判断设置Zset存在该值
* @param key
* @param value
* @return
*/
public Boolean hasZSetValueByRank(String key, String value) {
Long rank = null;
try {
rank = redisTemplate.opsForZSet().rank(key, value);
} catch (Exception e) {
log.error("【任务领域缓存服务】查询任务比对监控集合异常!", e);
}
return rank != null && rank >= 0;
}
/**
* 判断设置Zset存在该值
* @param key
* @param value
* @return
*/
public Boolean hasZSetValueByScore(String key, String value) {
Double score = null;
try {
score = redisTemplate.opsForZSet().score(key, value);
} catch (Exception e) {
log.error("【任务领域缓存服务】查询任务比对监控集合异常!", e);
}
return score != null;
}