package com.chushou.video.redis.dao;
import com.alibaba.fastjson.JSONObject;
import com.chushou.video.common.utils.CsLog;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author 基于spring和redis的redisTemplate工具类
* 针对所有的hash 都是以h开头的方法
* 针对所有的Set 都是以s开头的方法 含通用方法
* 针对所有的List 都是以l开头的方法
*/
@Repository
public class RedisDao {
@Resource
private RedisTemplate<String, Object> redisTemplate;
/**
* 查询redis key类型
*
* @param key
* @return
*/
public DataType ketType(String key) {
try {
return redisTemplate.type(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis type 异常! key: {}, error: {}", key, ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* 是否是第一次计数,并在第一次时设置有效期
*/
public boolean isFirstCounter(String key, long expireTime) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
long counter = entityIdCounter.incrementAndGet();
if (counter == 1) {// 初始设置过期时间
entityIdCounter.expire(expireTime, TimeUnit.SECONDS);
return true;
}
return false;
}
public boolean isFirstCounter(String key, Date expireAt) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
long counter = entityIdCounter.incrementAndGet();
if (counter == 1) {
// 初始设置过期时间
entityIdCounter.expireAt(expireAt);
return true;
}
return false;
}
/**
* 移除计数
*/
public void clearCounter(String key) {
deleteBatch(key);
}
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis expire 异常! key: {}, time: {}, error: {}", key, time, e.getMessage());
return false;
}
}
public boolean expireAt(String key, Date date) {
try {
if (date != null) {
redisTemplate.expireAt(key, date);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis expire 异常! key: {}, date: {}, error: {}", key, date, e.getMessage());
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 普通缓存获取和设置
*
* @param key 键
* @return 值
*/
public Object getAndSet(String key, Object value, long timeOut) {
Object obj = null;
try {
obj = redisTemplate.opsForValue().getAndSet(key, value);
if (timeOut > 0) {
expire(key, timeOut);
}
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis getAndSet 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
}
return obj;
}
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis set 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
return false;
}
}
/**
* 不存在时设置
*
* @param key 键
*/
public boolean setNE(String key, Object value) {
try {
Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value);
if (result != null) {
return result;
}
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis setNE 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
}
return false;
}
/**
* 不存在时设置,并设置有效期
*
* @param key 键
*/
public boolean setNEx(String key, Object value, Long expireTime) {
try {
Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, TimeUnit.SECONDS);
if (result != null) {
return result;
}
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis setNE 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
}
return false;
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param expire 过期时长(秒)
* @return true成功 false 失败
*/
public boolean setExpire(String key, Object value, long expire) {
try {
if (expire <= 0) {
expire = 3;
}
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redisTemplate.opsForValue().set操作失败, key: {}, value: {}, error: {}", key, value, ExceptionUtils.getStackTrace(e));
return false;
}
}
/**
* 普通缓存放入并在指定时间过期
*
* @param key 键
* @param value 值
* @param date 过期日期
* @return true成功 false 失败
*/
public boolean setExpireAt(String key, Object value, Date date) {
try {
redisTemplate.opsForValue().set(key, value);
redisTemplate.expireAt(key, date);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis setExpire 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
return false;
}
}
/**
* 不存在时设置
*
* @param key 键
*/
public boolean setExpireNE(String key, Object value, Long expTime) {
try {
Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value, expTime, TimeUnit.SECONDS);
if (result != null) {
return result;
}
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis setExpireNE 异常! key: {}, value: {}, error: {}", key, value, e.getMessage());
}
return false;
}
public Set<String> keysByPattern(String keyPrefix) {
return redisTemplate.keys(keyPrefix + "*");
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public Boolean existsKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis existsKey 异常! key: {}, error: {}", key, e.getMessage());
}
return false;
}
/**
* 递增
*
* @param key 键
* @return
*/
public Long incr(String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* 递增
*
* @param delta 要增加几(大于0)
*/
public Long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递增
*
* @param key 键
* @return
*/
public Long incrAndExpire(String key, long seconds) {
Long increment = redisTemplate.opsForValue().increment(key);
redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
return increment;
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
public boolean deleteByKey(String key) {
try {
return redisTemplate.delete(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis delete异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
public int deleteByPattern(String key) {
Set<String> keys = redisTemplate.keys(key + "*");
int delCount = 0;
for (String s : keys) {
Boolean delete = redisTemplate.delete(s);
if (Boolean.TRUE.equals(delete)) {
delCount++;
}
}
return delCount;
}
/**
* 删除缓存
*
* @param keyList 可以传一个值 或多个
*/
public Long deleteBatch(List<String> keyList) {
try {
if (!CollectionUtils.isEmpty(keyList)) {
return redisTemplate.delete(keyList);
}
return 0L;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis deleteBatch 异常! keyList: {}, error: {}", keyList, e.getMessage());
return 0L;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
public boolean deleteBatch(String... key) {
try {
if (key != null && key.length > 0) {
if (key.length == 1) {
return redisTemplate.delete(key[0]);
} else {
return redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key)) != null ? true : false;
}
}
return false;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis deleteBatch 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
public List<Object> multiGet(List<String> keys) {
return redisTemplate.opsForValue().multiGet(keys);
}
public void multiSet(Map<String, String> param) {
redisTemplate.opsForValue().multiSet(param);
}
// =----------------------------- hash start----------------------------
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项 只能是string
* @param value 值
* @return true 成功 false失败
*/
public boolean hPut(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis hPut 异常! key: {}, item: {}, error: {}", key, item, e.getMessage());
return false;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public boolean hPutExpire(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis hPutExpire 异常! key: {}, item: {}, error: {}", key, item, e.getMessage());
return false;
}
}
/**
* 向一张hash表中放入数据,并设置过期时间
*
* @param date 过期时间
*/
public boolean hPutExpireAt(String key, String item, Object value, Date date) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (date != null) {
expireAt(key, date);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis hPutExpireAt 异常! key: {}, item: {}, error: {}", key, item, e.getMessage());
return false;
}
}
/**
* hash get
*
* @param key
* @param hKey
* @return
*/
public Object hGet(String key, String hKey) {
try {
HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
return hashOperations.get(key, hKey);
} catch (Exception e) {
CsLog.error("redis hget 操作异常,key: {}, hkey: {}, e:{}", key, hKey, ExceptionUtils.getStackTrace(e));
}
return null;
}
public Map<String, Object> hGetAll(String key) {
try {
HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
return hashOperations.entries(key);
} catch (Exception e) {
CsLog.error("redis hGetAll 操作异常,key: {}, e:{}", key, ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* hM get
*
* @param key
* @param hKeyList string类型
* @return
*/
public List<Object> hMGet(String key, List<Object> hKeyList) {
try {
return redisTemplate.opsForHash().multiGet(key, hKeyList);
} catch (Exception e) {
CsLog.error("redis hMGet 操作异常,key: {}, hKeyList:{}, e:{}", key, JSONObject.toJSONString(hKeyList), ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* h批量put
*
* @param key
* @param map
*/
public void hPutAll(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hDel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hExist(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @return
*/
public double hIncr(String key, String item) {
return redisTemplate.opsForHash().increment(key, item, 1);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public double hIncr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public double hDecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
//======================================hash end===========================
//===============================list start=================================
/**
* 获取list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return
*/
public List<Object> listRange(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRange 异常! key: {}, start: {}, error: {}", key, start, e.getMessage());
return Collections.emptyList();
}
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return
*/
public Long listSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listSize 异常! key: {}, error: {}", key, e.getMessage());
return 0L;
}
}
/**
* 通过索引 获取list中的值
*
* @param key 键
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* @return
*/
public Object listGetByIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listGetByIndex 异常! key: {}, error: {}", key, e.getMessage());
return null;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* //@param time 时间(秒)
* @return
*/
public boolean listRightPush(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRightPush 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param timeOut 时间(秒)
* @return
*/
public boolean listRightPush(String key, Object value, long timeOut) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (timeOut > 0) {
expire(key, timeOut);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRightPush 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param value 时间(秒)
* @return
*/
private boolean listRightPushAll(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRightPushAll 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 左进
*/
public Object listLeftPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 右出
*/
public Object listRightPop(String key) {
return redisTemplate.opsForList().rightPop(key);
}
/**
* 批量右入
*
* @param key 键
* @param value 值
* @param time 时间(秒)
*/
private boolean listRightPushAll(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRightPushAll 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 根据修改list中的某条数据的索引
*
* @param key 键
* @param index 索引
* @param value 值
* @return
*/
public boolean listSetIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listSetIndex 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 移除N个值为value
*
* @param key 键
* @param count 移除多少个
* @param value 值
* @return 移除的个数
*/
public Long listRemove(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listRemove 异常! key: {}, error: {}", key, e.getMessage());
return 0L;
}
}
/**
* 在list中位置
*/
public Long listIndexOf(String key, Object value) {
try {
return redisTemplate.opsForList().indexOf(key, value);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis listIndexOf 异常! key: {}, error: {}", key, e.getMessage());
return null;
}
}
//-----------------list end ---------------------------------------------------
//======================================set start===========================
/**
* set 操作 add
*
* @param key
* @param values
*/
public Long sAdd(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis sAdd 操作异常! key: {}, values:{}, error: {}", key, values, ExceptionUtils.getStackTrace(e));
return 0L;
}
}
/**
* set操作移除
*
* @param key
* @param values
*/
public Long sRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis sRemove 操作异常! key: {}, values:{}, error: {}", key, values, ExceptionUtils.getStackTrace(e));
return 0L;
}
}
/**
* 获取集合所有成员
*
* @param key
*/
public Set<Object> sGet(String key) {
return redisTemplate.opsForSet().members(key);
}
public boolean sIsMember(String key, Object values) {
return redisTemplate.opsForSet().isMember(key, values);
}
public List<Object> sPop(String key, int count) {
return redisTemplate.opsForSet().pop(key, count);
}
/**
* set随机返回
*
* @param key
*/
public Object sRand(String key) {
return redisTemplate.opsForSet().randomMember(key);
}
/**
* 根据key获取Set中的所有值
*
* @param key 键
* @return
*/
public Set<Object> sMembers(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public boolean sExists(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis sExists 异常! key: {}, error: {}", key, e.getMessage());
return false;
}
}
/**
* 将set数据放入缓存
*
* @param key 键
* @param time 时间(秒)
* @param values 值 可以是多个
* @return 成功个数
*/
public long sAddAndExpire(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (count == null) {
return 0;
}
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis sSetAndTime 异常! key: {}, error: {}", key, e.getMessage());
return 0;
}
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis sGetSetSize 异常! key: {}, error: {}", key, e.getMessage());
return 0;
}
}
//======================================set end ======================================
///====================================== sorted set======================================
public boolean zAdd(String key, Object object, double score) {
try {
return redisTemplate.opsForZSet().add(key, object, score);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zAdd 异常! key: {}, error: {}", key, e.getMessage());
}
return false;
}
public Long zAddBatch(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
try {
return redisTemplate.opsForZSet().add(key, tuples);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zAddBatch 异常! key: {}, error: {}", key, e.getMessage());
}
return 0L;
}
// 删除分数区间内的值
public Long zRemove(String key, Object value) {
try {
return redisTemplate.opsForZSet().remove(key, value);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRemove 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
// 删除分数区间内的值
public Long zRemoveBatch(String key, Collection<Object> values) {
try {
return redisTemplate.opsForZSet().remove(key, values.toArray());
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRemove 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 删除分数区间内的值
*/
public Long zRemoveRangeByScore(String key, double minScore, double maxScore) {
try {
return redisTemplate.opsForZSet().removeRangeByScore(key, minScore, maxScore);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRemoveRangeByScore 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 获取在zSet中的分值
*/
public Double zScore(String key, Object object) {
try {
Double score = redisTemplate.opsForZSet().score(key, object);
if (score != null) {
return score;
}
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zScore 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 查询某个区间分支的元素个数
*/
public Long zCount(String key, double beginScore, double endScore) {
try {
return redisTemplate.opsForZSet().count(key, beginScore, endScore);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zCount 异常! key: {}, error: {}", key, e.getMessage());
}
return 0L;
}
/**
* 获取zSet集合成员数量
*/
public Long zCard(String key) {
try {
return redisTemplate.opsForZSet().zCard(key);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zCard 异常! key: {}, error: {}", key, e.getMessage());
}
return 0L;
}
public double zIncrScore(String key, Object value, double score) {
try {
return redisTemplate.opsForZSet().incrementScore(key, value, score);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zIncrScore 异常! key: {}, error: {}", key, e.getMessage());
}
return 0D;
}
public double zIncrScoreAndExpireAt(String key, String value, double score, Date date) {
try {
redisTemplate.execute(new SessionCallback() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
operations.opsForZSet().incrementScore(key, value, score);
operations.expireAt(key, date);
return null;
}
});
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zIncrScoreAndExpireAt 异常! key: {}, error: {}", key, e.getMessage());
}
return 0D;
}
/**
* 获取在zSet中排序
*/
public Long zReverseRank(String key, Object object) {
try {
return redisTemplate.opsForZSet().reverseRank(key, object);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zReverseRank 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 获取索引之间的对象(从高分到低分排序集)
*/
public Set<Object> zReverseRange(String key, long beginIndex, long endIndex) {
Set<Object> objects = null;
try {
objects = redisTemplate.opsForZSet().reverseRange(key, beginIndex, endIndex);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zReverseRange 异常! key: {}, error: {}", key, e.getMessage());
}
if (CollectionUtils.isEmpty(objects)) {
CsLog.warn("!!Redis查询,未查到ZSet数据, key={}, beginIndex: {}, endIndex: {}", key, beginIndex, endIndex);
}
return objects;
}
/**
* 按照索引的倒叙排序 索引start<=index<=end的元素子集
*/
public Set<ZSetOperations.TypedTuple<Object>> zReverseRangeWithScore(String key, long beginIndex, long endIndex) {
Set<ZSetOperations.TypedTuple<Object>> res = null;
try {
res = redisTemplate.opsForZSet().reverseRangeWithScores(key, beginIndex, endIndex);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zReverseRangeWithScore 异常! key: {}, error: {}", key, e.getMessage());
}
return res;
}
/**
* 取某一个区间分值的数据,只返回Id
*/
public Set<Object> zReverseRangeByScore(String key, double beginScore, double endScore) {
try {
return redisTemplate.opsForZSet().reverseRangeByScore(key, beginScore, endScore);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zReverseRangeByScore 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 取某一个区间分值的数据,只返回Id
*/
public Set<ZSetOperations.TypedTuple<Object>> zReverseRangeByScoreWithScores(String key, double beginScore, double endScore) {
try {
return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, beginScore, endScore);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis reverseRangeByScoreWithScores 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 获取范围从start以及end得分,包含end的索引,元素得分 高->低 排序
*/
public Set<Object> zReverseRangeByScore(String key, double startScore, double endScore, long offset, long count) {
return redisTemplate.opsForZSet().reverseRangeByScore(key, startScore, endScore, offset, count);
}
/**
* 获取对象排序
*/
public Long zRank(String key, Integer userId) {
try {
return redisTemplate.opsForZSet().rank(key, userId);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRank 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 取某一个区间的数据,分数从底到高
*/
public Set<Object> zRange(String key, long beginIndex, long endIndex) {
try {
return redisTemplate.opsForZSet().range(key, beginIndex, endIndex);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRange 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
/**
* 获取分数区间内的数据
*/
public Set<Object> zRangeByScore(String key, double min, double max) {
try {
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
} catch (Exception e) {
CsLog.dingAtTechnicalGroup("redis zRangeByScore 异常! key: {}, error: {}", key, e.getMessage());
}
return null;
}
//----------------------------- sorted set end----------------------------
/**
* 批量执行代码
*/
public void execute(SessionCallback sessionCallback) {
redisTemplate.execute(sessionCallback);
}
}
redis的方法 redis常用方法
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java中Redis常用的20个方法
通过本文的介绍,我们深入了解了Java中Redis的20个常用方法。这些方法涵盖了Redis的基本操作、数据结构的使用以及高级特性的应用。在
java redis 开发语言 Redis Java -
redission操作redis的api redis常用api方法
&nbs
redis API redis中文API redis命令 redis操作命令 redis