|  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.*; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description:Redis工具类 | 
|---|
|  |  |  | * @description:Redis工具类 | 
|---|
|  |  |  | * @author: swwheihei | 
|---|
|  |  |  | * @date:   2020年5月6日 下午8:27:29 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param score | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zAdd(String key, String value, double score) { | 
|---|
|  |  |  | public void zAdd(Object key, Object value, double score) { | 
|---|
|  |  |  | redisTemplate.opsForZSet().add(key, value, score); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zRemove(String key, String value) { | 
|---|
|  |  |  | public void zRemove(Object key, Object value) { | 
|---|
|  |  |  | redisTemplate.opsForZSet().remove(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param score | 
|---|
|  |  |  | * @param delta -1 表示减 1 表示加1 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zIncrScore(String key, String value, double score) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().incrementScore(key, value, score); | 
|---|
|  |  |  | public Double zIncrScore(Object key, Object value, double delta) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().incrementScore(key, value, delta); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zScore(String key, String value) { | 
|---|
|  |  |  | public Double zScore(Object key, Object value) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().score(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zRank(String key, String value) { | 
|---|
|  |  |  | public Long zRank(Object key, Object value) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rank(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zSize(String key) { | 
|---|
|  |  |  | public Long zSize(Object key) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().zCard(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> ZRange(String key, int start, int end) { | 
|---|
|  |  |  | public Set<Object> ZRange(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().range(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(String key, int start, int end) { | 
|---|
|  |  |  | public Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeWithScores(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zRevRange(String key, int start, int end) { | 
|---|
|  |  |  | public Set<String> zRevRange(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().reverseRange(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param max | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zSortRange(String key, int min, int max) { | 
|---|
|  |  |  | public Set<String> zSortRange(Object key, int min, int max) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeByScore(key, min, max); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 在键为 key 的 list中移除第一个元素 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object lLeftPop(String key) { | 
|---|
|  |  |  | return redisTemplate.opsForList().leftPop(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 在键为 key 的 list中移除、最后一个元素 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Object lrightPop(String key) { | 
|---|
|  |  |  | return redisTemplate.opsForList().rightPop(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 模糊查询 | 
|---|
|  |  |  | * @param key 键 | 
|---|
|  |  |  | * @return true / false | 
|---|
|  |  |  | 
|---|
|  |  |  | return new ArrayList<>(keys); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    ============================== 消息发送与订阅 ============================== | 
|---|
|  |  |  | public void convertAndSend(String channel, JSONObject msg) { | 
|---|
|  |  |  | //        redisTemplate.convertAndSend(channel, msg); | 
|---|
|  |  |  | redisTemplate.convertAndSend(channel, msg); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|