|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.utils.redis; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | 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.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.data.redis.core.*; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description:Redis工具类 | 
|---|
|  |  |  | * @author: songww | 
|---|
|  |  |  | * @description:Redis工具类 | 
|---|
|  |  |  | * @author: swwheihei | 
|---|
|  |  |  | * @date:   2020年5月6日 下午8:27:29 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | @SuppressWarnings(value = {"rawtypes", "unchecked"}) | 
|---|
|  |  |  | public class RedisUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | 
|---|
|  |  |  | return 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //    ============================== ZSet ============================== | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能;  zadd | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param score | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zAdd(Object key, Object value, double score) { | 
|---|
|  |  |  | redisTemplate.opsForZSet().add(key, value, score); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 删除元素 zrem | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void zRemove(Object key, Object value) { | 
|---|
|  |  |  | redisTemplate.opsForZSet().remove(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * score的增加or减少 zincrby | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @param delta -1 表示减 1 表示加1 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zIncrScore(Object key, Object value, double delta) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().incrementScore(key, value, delta); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询value对应的score   zscore | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Double zScore(Object key, Object value) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().score(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 判断value在zset中的排名  zrank | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param value | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zRank(Object key, Object value) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rank(key, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 返回集合的长度 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Long zSize(Object key) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().zCard(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询集合中指定顺序的值, 0 -1 表示获取全部的集合内容  zrange | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * 返回有序的集合,score小的在前面 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param start | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<Object> ZRange(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().range(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param start | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<ZSetOperations.TypedTuple<String>> zRangeWithScore(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeWithScores(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询集合中指定顺序的值  zrevrange | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * 返回有序的集合中,score大的在前面 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param start | 
|---|
|  |  |  | * @param end | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zRevRange(Object key, int start, int end) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().reverseRange(key, start, end); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据score的值,来获取满足条件的集合  zrangebyscore | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param key | 
|---|
|  |  |  | * @param min | 
|---|
|  |  |  | * @param max | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Set<String> zSortRange(Object key, int min, int max) { | 
|---|
|  |  |  | return redisTemplate.opsForZSet().rangeByScore(key, min, max); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    ============================== List ============================== | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | return 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 在键为 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 键 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<Object> keys(String key) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Set<String> set = redisTemplate.keys(key); | 
|---|
|  |  |  | Set<String> set = redisTemplate.keys(key); | 
|---|
|  |  |  | return new ArrayList<>(set); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 模糊查询 | 
|---|
|  |  |  | * @param query 查询参数 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | //    public List<Object> scan(String query) { | 
|---|
|  |  |  | //        List<Object> result = new ArrayList<>(); | 
|---|
|  |  |  | //        try { | 
|---|
|  |  |  | //            Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("field", | 
|---|
|  |  |  | //                    ScanOptions.scanOptions().match(query).count(1000).build()); | 
|---|
|  |  |  | //            while (cursor.hasNext()) { | 
|---|
|  |  |  | //                Map.Entry<Object,Object> entry = cursor.next(); | 
|---|
|  |  |  | //                result.add(entry.getKey()); | 
|---|
|  |  |  | //                Object key = entry.getKey(); | 
|---|
|  |  |  | //                Object valueSet = entry.getValue(); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //            //关闭cursor | 
|---|
|  |  |  | //            cursor.close(); | 
|---|
|  |  |  | //        } catch (Exception e) { | 
|---|
|  |  |  | //            e.printStackTrace(); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        return result; | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 模糊查询 | 
|---|
|  |  |  | * @param query 查询参数 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<Object> scan(String query) { | 
|---|
|  |  |  | Set<String> keys = (Set<String>) redisTemplate.execute((RedisCallback<Set<String>>) connection -> { | 
|---|
|  |  |  | Set<String> keysTmp = new HashSet<>(); | 
|---|
|  |  |  | Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(query).count(1000).build()); | 
|---|
|  |  |  | while (cursor.hasNext()) { | 
|---|
|  |  |  | keysTmp.add(new String(cursor.next())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return keysTmp; | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | //        Set<String> keys = (Set<String>) redisTemplate.execute(new RedisCallback<Set<String>>(){ | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            @Override | 
|---|
|  |  |  | //            public Set<String> doInRedis(RedisConnection connection) throws DataAccessException { | 
|---|
|  |  |  | //                Set<String> keysTmp = new HashSet<>(); | 
|---|
|  |  |  | //                Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(query).count(1000).build()); | 
|---|
|  |  |  | //                while (cursor.hasNext()) { | 
|---|
|  |  |  | //                    keysTmp.add(new String(cursor.next())); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            return keysTmp; | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return new ArrayList<>(keys); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    ============================== 消息发送与订阅 ============================== | 
|---|
|  |  |  | public void convertAndSend(String channel, JSONObject msg) { | 
|---|
|  |  |  | //        redisTemplate.convertAndSend(channel, msg); | 
|---|
|  |  |  | redisTemplate.convertAndSend(channel, msg); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|