redis中常用操作方法


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);
}
}


免責聲明!

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



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