chenjiahe
2022-06-27 a0ad14b0a0fb60604b0a9cf978a537e64479a8d4
提交 | 用户 | age
a0ad14 1 package com.hx.redis.templates.config;
C 2
3 import com.hx.redis.templates.manager.RedisManager;
4 import org.redisson.Redisson;
5 import org.redisson.api.RedissonClient;
6 import org.redisson.client.codec.StringCodec;
7 import org.redisson.config.Config;
8 import org.redisson.config.SingleServerConfig;
9 import org.redisson.spring.data.connection.RedissonConnectionFactory;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.beans.factory.annotation.Qualifier;
14 import org.springframework.beans.factory.annotation.Value;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.data.redis.core.RedisTemplate;
17 import org.springframework.data.redis.core.ZSetOperations;
18 import org.springframework.data.redis.support.atomic.RedisAtomicLong;
19 import org.springframework.stereotype.Component;
20 import org.springframework.util.CollectionUtils;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.TimeUnit;
26
27 /**
28  * <h1>RedisUtil 操作工具类</h1>
29  * @author CJH
30  */
31 @Component
32 public class RedisTempsUtil {
33
34     private Logger logger = LoggerFactory.getLogger(RedisTempsUtil.class);
35
36     @Autowired
37     private RedisManager redisManager;
38
39     @Autowired
40     private RedisTemplate redisTemplate;
41
42     /**
43      * 指定缓存失效时间
44      *
45      * @param key  键
46      * @param time 时间(秒)
47      * @return
48      */
49     public boolean expire(String key, long time) {
50         try {
51             if (time > 0) {
52                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
53             }
54             return true;
55         } catch (Exception e) {
56             e.printStackTrace();
57             return false;
58         }
59     }
60
61
62     /**
63      * 指定缓存失效时间
64      *
65      * @param key      键
66      * @param time     时间
67      * @param timeUnit 时间类型
68      * @return
69      */
70     public boolean expire(String key, long time, TimeUnit timeUnit) {
71         try {
72             if (time > 0) {
73                 redisTemplate.expire(key, time, timeUnit);
74             }
75             return true;
76         } catch (Exception e) {
77             e.printStackTrace();
78             return false;
79         }
80     }
81
82     /**
83      * 根据key 获取过期时间
84      *
85      * @param key 键 不能为null
86      * @return 时间(秒) 返回0代表为永久有效
87      */
88     public long getExpire(String key) {
89         return redisTemplate.getExpire(key, TimeUnit.SECONDS);
90     }
91
92     /**
93      * 判断key是否存在
94      *
95      * @param key 键
96      * @return true 存在 false不存在
97      */
98     public boolean hasKey(String key) {
99         try {
100             return redisTemplate.hasKey(key);
101         } catch (Exception e) {
102             e.printStackTrace();
103             return false;
104         }
105     }
106
107     /**
108      * 删除缓存
109      *
110      * @param key 可以传一个值 或多个
111      */
112     @SuppressWarnings("unchecked")
113     public void del(String... key) {
114         if (key != null && key.length > 0) {
115             if (key.length == 1) {
116                 redisTemplate.delete(key[0]);
117             } else {
118                 redisTemplate.delete(CollectionUtils.arrayToList(key));
119             }
120         }
121     }
122
123     // ============================String=============================
124
125     /**
126      * 普通缓存获取
127      *
128      * @param key 键
129      * @return 值
130      */
131     public Object get(String key) {
132         return key == null ? null : redisTemplate.opsForValue().get(key);
133     }
134
135     /**
136      * 普通缓存放入
137      *
138      * @param key   键
139      * @param value 值
140      * @return true成功 false失败
141      */
142     public boolean set(String key, Object value) {
143         try {
144             redisTemplate.opsForValue().set(key, value);
145             return true;
146         } catch (Exception e) {
147             e.printStackTrace();
148             return false;
149         }
150
151     }
152
153
154     /**
155      * 普通缓存放入
156      *
157      * @param key   键
158      * @param value 值
159      * @return true成功 false失败
160      */
161     public boolean set(String key, Object value,int db) {
162         try {
163             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
164             redisTemplate.opsForValue().set(key, value);
165             return true;
166         } catch (Exception e) {
167             e.printStackTrace();
168             return false;
169         }
170
171     }
172
173
174     /**
175      * 普通缓存放入并设置时间
176      *
177      * @param key   键
178      * @param value 值
179      * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
180      * @return true成功 false 失败
181      */
182     public boolean set(String key, Object value, long time) {
183         try {
184             if (time > 0) {
185                 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
186             } else {
187                 set(key, value);
188             }
189             return true;
190         } catch (Exception e) {
191             e.printStackTrace();
192             return false;
193         }
194     }
195
196     /**
197      * 普通缓存放入并设置时间,并增加时间类型选择
198      *
199      * @param key      键
200      * @param value    值
201      * @param time     时间 time要大于0 如果time小于等于0 将设置无限期
202      * @param timeUnit 时间类型
203      * @return
204      */
205     public boolean set(String key, Object value, long time, TimeUnit timeUnit) {
206         try {
207             if (time > 0) {
208                 redisTemplate.opsForValue().set(key, value, time, timeUnit);
209             } else {
210                 set(key, value);
211             }
212             return true;
213         } catch (Exception e) {
214             e.printStackTrace();
215             return false;
216         }
217     }
218
219     /**
220      * 递增
221      *
222      * @param key   键
223      * @param delta 要增加几(大于0)
224      * @return
225      */
226     public long incr(String key, long delta) {
227         if (delta < 0) {
228             throw new RuntimeException("递增因子必须大于0");
229         }
230         return redisTemplate.opsForValue().increment(key, delta);
231     }
232
233     /**
234      * 递减
235      *
236      * @param key   键
237      * @param delta 要减少几(小于0)
238      * @return
239      */
240     public long decr(String key, long delta) {
241         if (delta < 0) {
242             throw new RuntimeException("递减因子必须大于0");
243         }
244         return redisTemplate.opsForValue().increment(key, -delta);
245     }
246
247     // ================================Map=================================
248
249     /**
250      * HashGet
251      *
252      * @param key  键 不能为null
253      * @param item 项 不能为null
254      * @return 值
255      */
256     public Object hget(String key, String item) {
257         return redisTemplate.opsForHash().get(key, item);
258     }
259
260     /**
261      * 获取hashKey对应的所有键值
262      *
263      * @param key 键
264      * @return 对应的多个键值
265      */
266     public Map<Object, Object> hmget(String key) {
267         return redisTemplate.opsForHash().entries(key);
268     }
269
270     /**
271      * HashSet
272      *
273      * @param key 键
274      * @param map 对应多个键值
275      * @return true 成功 false 失败
276      */
277     public boolean hmset(String key, Map<String, Object> map) {
278         try {
279             redisTemplate.opsForHash().putAll(key, map);
280             return true;
281         } catch (Exception e) {
282             e.printStackTrace();
283             return false;
284         }
285     }
286
287     /**
288      * HashSet 并设置时间
289      *
290      * @param key  键
291      * @param map  对应多个键值
292      * @param time 时间(秒)
293      * @return true成功 false失败
294      */
295     public boolean hmset(String key, Map<String, Object> map, long time) {
296         try {
297             redisTemplate.opsForHash().putAll(key, map);
298             if (time > 0) {
299                 expire(key, time);
300             }
301             return true;
302         } catch (Exception e) {
303             e.printStackTrace();
304             return false;
305         }
306     }
307
308     /**
309      * 向一张hash表中放入数据,如果不存在将创建
310      *
311      * @param key   键
312      * @param item  项
313      * @param value 值
314      * @return true 成功 false失败
315      */
316     public boolean hset(String key, String item, Object value) {
317         try {
318             redisTemplate.opsForHash().put(key, item, value);
319             return true;
320         } catch (Exception e) {
321             e.printStackTrace();
322             return false;
323         }
324     }
325
326     /**
327      * 向一张hash表中放入数据,如果不存在将创建
328      *
329      * @param key   键
330      * @param item  项
331      * @param value 值
332      * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
333      * @return true 成功 false失败
334      */
335     public boolean hset(String key, String item, Object value, long time) {
336         try {
337             redisTemplate.opsForHash().put(key, item, value);
338             if (time > 0) {
339                 expire(key, time);
340             }
341             return true;
342         } catch (Exception e) {
343             e.printStackTrace();
344             return false;
345         }
346     }
347
348     /**
349      * 删除hash表中的值
350      *
351      * @param key  键 不能为null
352      * @param item 项 可以使多个 不能为null
353      */
354     public void hdel(String key, Object... item) {
355         redisTemplate.opsForHash().delete(key, item);
356     }
357
358     /**
359      * 判断hash表中是否有该项的值
360      *
361      * @param key  键 不能为null
362      * @param item 项 不能为null
363      * @return true 存在 false不存在
364      */
365     public boolean hHasKey(String key, String item) {
366         return redisTemplate.opsForHash().hasKey(key, item);
367     }
368
369     /**
370      * hash递增 如果不存在,就会创建一个 并把新增后的值返回
371      *
372      * @param key  键
373      * @param item 项
374      * @param by   要增加几(大于0)
375      * @return
376      */
377     public double hincr(String key, String item, double by) {
378         return redisTemplate.opsForHash().increment(key, item, by);
379     }
380
381     /**
382      * hash递减
383      *
384      * @param key  键
385      * @param item 项
386      * @param by   要减少记(小于0)
387      * @return
388      */
389     public double hdecr(String key, String item, double by) {
390         return redisTemplate.opsForHash().increment(key, item, -by);
391     }
392
393     // ============================set=============================
394
395     /**
396      * 根据key获取Set中的所有值
397      *
398      * @param key 键
399      * @return
400      */
401     public Set<Object> sGet(String key) {
402         try {
403             return redisTemplate.opsForSet().members(key);
404         } catch (Exception e) {
405             e.printStackTrace();
406             return null;
407         }
408     }
409
410     /**
411      * 根据key获取Set中的指定几个随机内容
412      *
413      * @param key  键
414      * @param size 个数
415      * @return
416      */
417     public List sRandomGet(String key, Integer size) {
418         try {
419             return redisTemplate.opsForSet().randomMembers(key, size);
420         } catch (Exception e) {
421             e.printStackTrace();
422             return null;
423         }
424     }
425
426     /**
427      * 根据value从一个set中查询,是否存在
428      *
429      * @param key   键
430      * @param value 值
431      * @return true 存在 false不存在
432      */
433     public boolean sHasKey(String key, Object value) {
434         try {
435             return redisTemplate.opsForSet().isMember(key, value);
436         } catch (Exception e) {
437             e.printStackTrace();
438             return false;
439         }
440     }
441
442     /**
443      * 将数据放入set缓存
444      *
445      * @param key    键
446      * @param values 值 可以是多个
447      * @return 成功个数
448      */
449     public long sSet(String key, Object... values) {
450         try {
451             return redisTemplate.opsForSet().add(key, values);
452         } catch (Exception e) {
453             e.printStackTrace();
454             return 0;
455         }
456     }
457
458     /**
459      * 将set数据放入缓存
460      *
461      * @param key    键
462      * @param time   时间(秒)
463      * @param values 值 可以是多个
464      * @return 成功个数
465      */
466     public long sSetAndTime(String key, long time, Object... values) {
467         try {
468             Long count = redisTemplate.opsForSet().add(key, values);
469             if (time > 0) {
470                 expire(key, time);
471             }
472             return count;
473         } catch (Exception e) {
474             e.printStackTrace();
475             return 0;
476         }
477     }
478
479     /**
480      * 获取set缓存的长度
481      *
482      * @param key 键
483      * @return
484      */
485     public long sGetSetSize(String key) {
486         try {
487             return redisTemplate.opsForSet().size(key);
488         } catch (Exception e) {
489             e.printStackTrace();
490             return 0;
491         }
492     }
493
494     /**
495      * 移除值为value的
496      *
497      * @param key    键
498      * @param values 值 可以是多个
499      * @return 移除的个数
500      */
501     public long setRemove(String key, Object... values) {
502         try {
503             Long count = redisTemplate.opsForSet().remove(key, values);
504             return count;
505         } catch (Exception e) {
506             e.printStackTrace();
507             return 0;
508         }
509     }
510     // ===============================list=================================
511
512     /**
513      * 获取list缓存的内容
514      *
515      * @param key   键
516      * @param start 开始
517      * @param end   结束 0 到 -1代表所有值
518      * @return
519      */
520     public List<Object> lGet(String key, long start, long end) {
521         try {
522             return redisTemplate.opsForList().range(key, start, end);
523         } catch (Exception e) {
524             e.printStackTrace();
525             return null;
526         }
527     }
528
529     /**
530      * 获取list缓存的长度
531      *
532      * @param key 键
533      * @return
534      */
535     public long lGetListSize(String key) {
536         try {
537             return redisTemplate.opsForList().size(key);
538         } catch (Exception e) {
539             e.printStackTrace();
540             return 0;
541         }
542     }
543
544     /**
545      * 通过索引 获取list中的值
546      *
547      * @param key   键
548      * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
549      * @return
550      */
551     public Object lGetIndex(String key, long index) {
552         try {
553             return redisTemplate.opsForList().index(key, index);
554         } catch (Exception e) {
555             e.printStackTrace();
556             return null;
557         }
558     }
559
560     /**
561      * 将list放入缓存
562      *
563      * @param key   键
564      * @param value 值
565      * @return
566      */
567     public boolean lSet(String key, Object value) {
568         try {
569             redisTemplate.opsForList().rightPush(key, value);
570             return true;
571         } catch (Exception e) {
572             e.printStackTrace();
573             return false;
574         }
575     }
576
577     /**
578      * 将list放入缓存
579      *
580      * @param key   键
581      * @param value 值
582      * @param time  时间(秒)
583      * @return
584      */
585     public boolean lSet(String key, Object value, long time) {
586         try {
587             redisTemplate.opsForList().rightPush(key, value);
588             if (time > 0) {
589                 expire(key, time);
590             }
591             return true;
592         } catch (Exception e) {
593             e.printStackTrace();
594             return false;
595         }
596     }
597
598     /**
599      * 将list放入缓存
600      *
601      * @param key   键
602      * @param value 值
603      * @return
604      */
605     public boolean lSetAll(String key, List<Object> value) {
606         try {
607             redisTemplate.opsForList().rightPushAll(key, value);
608             return true;
609         } catch (Exception e) {
610             e.printStackTrace();
611             return false;
612         }
613     }
614
615     /**
616      * 将list放入缓存
617      *
618      * @param key   键
619      * @param value 值
620      * @param time  时间(秒)
621      * @return
622      */
623     public boolean lSetAll(String key, List<Object> value, long time) {
624         try {
625             // 设置超时时间 原子化
626             redisTemplate.opsForList().rightPushAll(key, value);
627             if (time > 0) {
628                 expire(key, time);
629             }
630             return true;
631         } catch (Exception e) {
632             e.printStackTrace();
633             return false;
634         }
635     }
636
637     /**
638      * 根据索引修改list中的某条数据
639      *
640      * @param key   键
641      * @param index 索引
642      * @param value 值
643      * @return
644      */
645     public boolean lUpdateIndex(String key, long index, Object value) {
646         try {
647             redisTemplate.opsForList().set(key, index, value);
648             return true;
649         } catch (Exception e) {
650             e.printStackTrace();
651             return false;
652         }
653     }
654
655     /**
656      * 移除N个值为value
657      *
658      * @param key   键
659      * @param count 移除多少个
660      * @param value 值
661      * @return 移除的个数
662      */
663     public long lRemove(String key, long count, Object value) {
664         try {
665             Long remove = redisTemplate.opsForList().remove(key, count, value);
666             return remove;
667         } catch (Exception e) {
668             e.printStackTrace();
669             return 0;
670         }
671     }
672
673
674     /**
675      * 根据给定的布隆过滤器添加值
676      *
677      * @param bloomFilterHelper bloom布隆过滤器解析类
678      * @param key               redis 的key
679      * @param value             redis 的value
680      * @param <T>               值的类型
681      */
682     /*public <T> void addByBloomFilter(BloomFilterHelper<T> bloomFilterHelper, String key, T value) {
683         Preconditions.checkArgument(bloomFilterHelper != null, "bloomFilterHelper不能为空");
684         int[] offset = bloomFilterHelper.murmurHashOffset(value);
685         for (int i : offset) {
686             redisTemplate.opsForValue().setBit(key, i, true);
687         }
688     }*/
689
690
691     /**
692      * 根据给定的布隆过滤器判断值是否存在
693      *
694      * @param bloomFilterHelper bloom布隆过滤器解析类
695      * @param key               redis 的key
696      * @param value             redis 的value
697      * @param <T>               值的类型
698      * @return 存在 true
699      */
700    /* public <T> boolean includeByBloomFilter(BloomFilterHelper<T> bloomFilterHelper, String key, T value) {
701         Preconditions.checkArgument(bloomFilterHelper != null, "bloomFilterHelper不能为空");
702         int[] offset = bloomFilterHelper.murmurHashOffset(value);
703         for (int i : offset) {
704             if (!redisTemplate.opsForValue().getBit(key, i)) {
705                 return false;
706             }
707         }
708         return true;
709     }*/
710
711
712     /**
713      * zset 添加元素
714      *
715      * @param key
716      * @param time
717      * @param tuples
718      * @return
719      */
720     public long zsSetAndTime(String key, long time, Set<ZSetOperations.TypedTuple<Object>> tuples) {
721         try {
722             Long count = redisTemplate.opsForZSet().add(key, tuples);
723             if (time > 0) {
724                 expire(key, time);
725             }
726             return count;
727         } catch (Exception e) {
728             e.printStackTrace();
729             return 0;
730         }
731     }
732
733     /**
734      * zset 添加元素
735      *
736      * @param key
737      * @param tuples
738      * @return
739      */
740     public long zsSetAndTime(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
741         try {
742             Long count = redisTemplate.opsForZSet().add(key, tuples);
743             return count;
744         } catch (Exception e) {
745             e.printStackTrace();
746             return 0;
747         }
748     }
749
750     /**
751      * zs 移除元素
752      *
753      * @param key
754      * @param values
755      * @return
756      */
757     public long zsRemove(String key, Object... values) {
758         try {
759             Long remove = redisTemplate.opsForZSet().remove(key, values);
760             return remove;
761         } catch (Exception e) {
762             e.printStackTrace();
763             return 0;
764         }
765     }
766
767
768     /**
769      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列
770      *
771      * @param key   键
772      * @param start 起始位置 0
773      * @param end   末尾位置 -1
774      * @return 0 -1 返回按分数递增的顺序集合  仅返回 key
775      */
776     public Set zsGet(String key, long start, long end) {
777         try {
778             return redisTemplate.opsForZSet().range(key, start, end);
779         } catch (Exception e) {
780             e.printStackTrace();
781             return null;
782         }
783     }
784
785
786     /**
787      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列
788      *
789      * @param key   键
790      * @param start 起始位置 0
791      * @param end   末尾位置 -1
792      * @return 0 -1 返回按分数递增的顺序集合,返回成员对象
793      */
794     public Set zsGetWithScores(String key, long start, long end) {
795         try {
796             return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
797         } catch (Exception e) {
798             e.printStackTrace();
799             return null;
800         }
801     }
802
803     /**
804      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递减(从大到小)顺序排列
805      *
806      * @param key   键
807      * @param start 起始位置 0
808      * @param end   末尾位置 -1
809      * @return 0 -1 返回按分数递增的顺序集合  仅返回 key
810      */
811     public Set zsGetReverse(String key, long start, long end) {
812         try {
813             return redisTemplate.opsForZSet().reverseRange(key, start, end);
814         } catch (Exception e) {
815             e.printStackTrace();
816             return null;
817         }
818     }
819
820
821     /**
822      * 通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递减(从大到小)顺序排列
823      *
824      * @param key   键
825      * @param start 起始位置 0
826      * @param end   末尾位置 -1
827      * @return 0 -1 返回按分数递增的顺序集合,返回成员对象
828      */
829     public Set zsGetReverseWithScores(String key, long start, long end) {
830         try {
831             return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
832         } catch (Exception e) {
833             e.printStackTrace();
834             return null;
835         }
836     }
837
838     /**
839      * 切换数据库
840      *
841      * @return
842      */
843     public RedisTemplate switchDatabase(Integer dbIndex) {
844         logger.info("重新建立redis数据库连接开始");
845         Config config = new Config();
846         config.setCodec(StringCodec.INSTANCE);
847         SingleServerConfig singleConfig = config.useSingleServer();
848         singleConfig.setAddress("redis://120.53.235.63:6378");
849         singleConfig.setPassword("gengzi666");
850         singleConfig.setDatabase(dbIndex);
851         RedissonClient redissonClient = Redisson.create(config);
852         RedissonConnectionFactory redisConnectionFactory = new RedissonConnectionFactory(redissonClient);
853         redisTemplate.setConnectionFactory(redisConnectionFactory);
854         logger.info("重新建立redis数据库连接结束");
855         return redisTemplate;
856     }
857
858     /**
859      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列
860      *
861      * @param key   键
862      * @param start 起始位置 0
863      * @param end   末尾位置 -1
864      * @return 0 -1 返回按分数递增的顺序集合  仅返回 key
865      */
866     public Set zsGet(String key, long start, long end, Integer db) {
867         try {
868             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
869             return redisTemplate.opsForZSet().range(key, start, end);
870         } catch (Exception e) {
871             e.printStackTrace();
872             return null;
873         }
874     }
875
876
877     /**
878      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列
879      *
880      * @param key   键
881      * @param start 起始位置 0
882      * @param end   末尾位置 -1
883      * @return 0 -1 返回按分数递增的顺序集合,返回成员对象
884      */
885     public Set zsGetWithScores(String key, long start, long end, Integer db) {
886         try {
887             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
888             return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
889         } catch (Exception e) {
890             e.printStackTrace();
891             return null;
892         }
893     }
894
895     /**
896      * 通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递减(从大到小)顺序排列
897      *
898      * @param key   键
899      * @param start 起始位置 0
900      * @param end   末尾位置 -1
901      * @return 0 -1 返回按分数递增的顺序集合  仅返回 key
902      */
903     public Set zsGetReverse(String key, long start, long end, Integer db) {
904         try {
905             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
906             return redisTemplate.opsForZSet().reverseRange(key, start, end);
907         } catch (Exception e) {
908             e.printStackTrace();
909             return null;
910         }
911     }
912
913
914     /**
915      * 通过索引区间返回有序集合成指定区间内的成员对象,其中有序集成员按分数值递减(从大到小)顺序排列
916      *
917      * @param key   键
918      * @param start 起始位置 0
919      * @param end   末尾位置 -1
920      * @return 0 -1 返回按分数递增的顺序集合,返回成员对象
921      */
922     public Set zsGetReverseWithScores(String key, long start, long end, Integer db) {
923         try {
924             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
925             return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
926         } catch (Exception e) {
927             e.printStackTrace();
928             return null;
929         }
930     }
931
932     /**
933      * zset 添加元素
934      *
935      * @param key
936      * @param time
937      * @param tuples
938      * @return
939      */
940     public long zsSetAndTime(String key, long time, Set<ZSetOperations.TypedTuple<Object>> tuples, Integer db) {
941         try {
942             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
943             Long count = redisTemplate.opsForZSet().add(key, tuples);
944             if (time > 0) {
945                 expire(key, time);
946             }
947             return count;
948         } catch (Exception e) {
949             e.printStackTrace();
950             return 0;
951         }
952     }
953
954     /**
955      * zset 添加元素
956      *
957      * @param key
958      * @param tuples
959      * @return
960      */
961     public long zsSetAndTime(String key, Set<ZSetOperations.TypedTuple<Object>> tuples, Integer db) {
962         try {
963             // 根据db序号,获取对应的 RedisTemplate
964             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
965             Long count = redisTemplate.opsForZSet().add(key, tuples);
966             return count;
967         } catch (Exception e) {
968             e.printStackTrace();
969             return 0;
970         }
971     }
972
973     /**
974      * zs 移除元素
975      *
976      * @param key
977      * @param values
978      * @return
979      */
980     public long zsRemove(String key, Integer db, Object... values) {
981         try {
982             RedisTemplate redisTemplate = redisManager.getRedisTemplate(db);
983             Long remove = redisTemplate.opsForZSet().remove(key, values);
984             return remove;
985         } catch (Exception e) {
986             e.printStackTrace();
987             return 0;
988         }
989     }
990
991 }