zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
package com.shlanbao.tzsc.utils.tools;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
 
@SuppressWarnings("unchecked")
public class RedisUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(RedisUtil.class);
     
        private static RedisTemplate<String, Object> redisTemplate = ApplicationContextUtil.getBean("redisTemplate", RedisTemplate.class);
     
        private static StringRedisTemplate stringRedisTemplate = ApplicationContextUtil.getBean("stringRedisTemplate", StringRedisTemplate.class);
     
        private static String CACHE_PREFIX;
     
        private static boolean CACHE_CLOSED;
     
        private RedisUtil() {
     
        }
     
        @SuppressWarnings("rawtypes")
        private static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            String str = obj.toString();
            if ("".equals(str.trim())) {
            return true;
            }
            return false;
        }
        if (obj instanceof List) {
            List<Object> list = (List<Object>) obj;
            if (list.isEmpty()) {
            return true;
            }
            return false;
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            if (map.isEmpty()) {
            return true;
            }
            return false;
        }
        if (obj instanceof Set) {
            Set set = (Set) obj;
            if (set.isEmpty()) {
            return true;
            }
            return false;
        }
        if (obj instanceof Object[]) {
            Object[] objs = (Object[]) obj;
            if (objs.length <= 0) {
            return true;
            }
            return false;
        }
        return false;
        }
        /**
         * 构建缓存key值
         * @param key    缓存key
         * @return
         */
        private static String buildKey(String key) {
        if (CACHE_PREFIX == null || "".equals(CACHE_PREFIX)) {
            return key;
        }
        return CACHE_PREFIX + ":" + key;
        }
        /**
         * 返回缓存的前缀
         * @return CACHE_PREFIX_FLAG
         */
        public static String getCachePrefix() {
        return CACHE_PREFIX;
        }
        /**
         * 设置缓存的前缀
         * @param cachePrefix
         */
        public static void setCachePrefix(String cachePrefix) {
        if (cachePrefix != null && !"".equals(cachePrefix.trim())) {
            CACHE_PREFIX = cachePrefix.trim();
        }
        }
        /**
         * 关闭缓存
         * @return    true:成功 
         *         false:失败
         */
        public static boolean close() {
        LOG.debug(" cache closed ! ");
        CACHE_CLOSED = true;
        return true;
        }
        /**
         * 打开缓存
         * @return    true:存在
         *         false:不存在
         */
        public static boolean openCache() {
        CACHE_CLOSED = false;
        return true;
        }
        /**
         * 检查缓存是否开启
         * @return    true:已关闭 
         *         false:已开启
         */
        public static boolean isClose() {
        return CACHE_CLOSED;
        }
        /**
         * 判断key值是否存在
         * @param key    缓存的key
         * @return    true:存在
         *         false:不存在
         */
        public static boolean hasKey(String key) {
        LOG.debug(" hasKey key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return false;
            }
            key = buildKey(key);
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
        /**
         * 匹配符合正则的key
         * @param patternKey
         * @return key的集合
         */
        public static Set<String> keys(String patternKey) {
        LOG.debug(" keys key :{}", patternKey);
        try {
            if (isClose() || isEmpty(patternKey)) {
            return Collections.emptySet();
            }
            return redisTemplate.keys(patternKey);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return Collections.emptySet();
        }
     
        /**
         * 根据key删除缓存
         * @param key
         * @return    true:成功 
         *         false:失败
         */
        public static boolean del(String... key) {
        LOG.debug(" delete key :{}", key.toString());
        try {
            if (isClose() || isEmpty(key)) {
            return false;
            }
            Set<String> keySet = new HashSet<>();
            for (String str : key) {
            keySet.add(buildKey(str));
            }
            redisTemplate.delete(keySet);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 根据key删除缓存
         * @param key
         * @return    true:成功 
         *         false:失败
         */
        public static boolean delPattern(String key) {
        LOG.debug(" delete Pattern keys :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.delete(redisTemplate.keys(key));
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 删除一组key值
         * @param keys
         * @return    true:成功 
         *         false:失败
         */
        public static boolean del(Set<String> keys) {
        LOG.debug(" delete keys :{}", keys.toString());
        try {
            if (isClose() || isEmpty(keys)) {
            return false;
            }
            Set<String> keySet = new HashSet<>();
            for (String str : keys) {
            keySet.add(buildKey(str));
            }
            redisTemplate.delete(keySet);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 设置过期时间
         * @param key    缓存key
         * @param seconds    过期秒数
         * @return    true:成功 
         *         false:失败
         */
        public static boolean setExp(String key, long seconds) {
        LOG.debug(" setExp key :{}, seconds: {}", key, seconds);
        try {
            if (isClose() || isEmpty(key) || seconds > 0) {
            return false;
            }
            key = buildKey(key);
            return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 查询过期时间
         * @param key    缓存key
         * @return    秒数
         */
        public static Long getExpire(String key) {
        LOG.debug(" getExpire key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return 0L;
            }
            key = buildKey(key);
            return redisTemplate.getExpire(key, TimeUnit.SECONDS);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return 0L;
        }
     
        /**
         * 缓存存入key-value
         * @param key    缓存键
         * @param value    缓存值
         * @return    true:成功 
         *         false:失败
         */
        public static boolean setString(String key, String value) {
        LOG.debug(" setString key :{}, value: {}", key, value);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            stringRedisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 缓存存入key-value
         * @param key    缓存键
         * @param value    缓存值
         * @param seconds    秒数
         * @return    true:成功 
         *         false:失败
         */
        public static boolean setString(String key, String value, long seconds) {
        //LOG.debug(" setString key :{}, value: {}, timeout:{}", key, value, seconds);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 根据key取出String value
         * @param key    缓存key值
         * @return    String    缓存的String
         */
        public static String getString(String key) {
        LOG.debug(" getString key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return stringRedisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 去的缓存中的最大值并+1
         * @param key    缓存key值
         * @return    long    缓存中的最大值+1
         */
        public static long incr(String key) {
        LOG.debug(" incr key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return 0;
            }
            key = buildKey(key);
            return redisTemplate.opsForValue().increment(key, 1);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return 0;
        }
     
        /**
         * 缓存中存入序列化的Object对象
         * @param <T>
         * @param key    缓存key
         * @param obj    存入的序列化对象
         * @return    true:成功 
         *         false:失败
         */
        public static boolean set(String key, Object obj) {
        LOG.debug(" set key :{}, value:{}", key, obj);
        try {
            if (isClose() || isEmpty(key) || isEmpty(obj)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForValue().set(key, obj);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 缓存中存入序列化的Object对象
         * @param <T>
         * @param key    缓存key
         * @param obj    存入的序列化对象
         * @return    true:成功 
         *         false:失败
         */
        public static boolean setObj(String key, Object obj, long seconds) {
        //LOG.debug(" set key :{}, value:{}, seconds:{}", key, obj, seconds);
        try {
            if (isClose() || isEmpty(key) || isEmpty(obj)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForValue().set(key, obj);
            if (seconds > 0) {
            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 取出缓存中存储的序列化对象
         * @param key    缓存key
         * @param clazz    对象类
         * @return <T>    序列化对象
         */
        public static <T> T getObj(String key, Class<T> clazz) {
        LOG.debug(" get key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return (T) redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 存入Map数组
         * @param <T>
         * @param key    缓存key
         * @param map    缓存map
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setMap(String key, Map<String, T> map) {
        try {
            if (isClose() || isEmpty(key) || isEmpty(map)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 取出缓存的map
         * @param key    缓存key
         * @return map    缓存的map
         */
        @SuppressWarnings("rawtypes")
        public static Map getMap(String key) {
        LOG.debug(" getMap key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 查询缓存的map的集合大小
         * @param key    缓存key
         * @return int    缓存map的集合大小
         */
        public static long getMapSize(String key) {
        LOG.debug(" getMap key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return 0;
            }
            key = buildKey(key);
            return redisTemplate.opsForHash().size(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return 0;
        }
     
        
        /**
         * 根据key以及hashKey取出对应的Object对象
         * @param key    缓存key
         * @param hashKey    对应map的key
         * @return object    map中的对象
         */
        public static Object getMapKey(String key, String hashKey) {
        LOG.debug(" getMapkey :{}, hashKey:{}", key, hashKey);
        try {
            if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
            return null;
            }
            key = buildKey(key);
            return redisTemplate.opsForHash().get(key, hashKey);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 取出缓存中map的所有key值
         * @param key    缓存key
         * @return Set<String> map的key值合集
         */
        public static Set<Object> getMapKeys(String key) {
        LOG.debug(" getMapKeys key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return redisTemplate.opsForHash().keys(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 删除map中指定的key值
         * @param key    缓存key
         * @param hashKey    map中指定的hashKey
         * @return    true:成功 
         *         false:失败
         */
        public static boolean delMapKey(String key, String hashKey) {
        LOG.debug(" delMapKey key :{}, hashKey:{}", key, hashKey);
        try {
            if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForHash().delete(key, hashKey);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 存入Map数组
         * @param <T>
         * @param key    缓存key
         * @param map    缓存map
         * @param seconds    秒数
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setMapExp(String key, Map<String, T> map, long seconds) {
        //LOG.debug(" setMapExp key :{}, value: {}, seconds:{}", key, map, seconds);
        try {
            if (isClose() || isEmpty(key) || isEmpty(map)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForHash().putAll(key, map);
            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * map中加入新的key
         * @param <T>
         * @param key    缓存key
         * @param hashKey    map的Key值
         * @param value    map的value值
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean addMap(String key, String hashKey, T value) {
        //LOG.debug(" addMap key :{}, hashKey: {}, value:{}", key, hashKey, value);
        try {
            if (isClose() || isEmpty(key) || isEmpty(hashKey) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForHash().put(key, hashKey, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 缓存存入List
         * @param <T>
         * @param key    缓存key
         * @param list    缓存List
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setList(String key, List<T> list) {
        LOG.debug(" setList key :{}, list: {}", key, list);
        try {
            if (isClose() || isEmpty(key) || isEmpty(list)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForList().leftPushAll(key, list.toArray());
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 根据key值取出对应的list合集
         * @param key    缓存key
         * @return List<Object> 缓存中对应的list合集
         */
        public static <V> List<V> getList(String key) {
        LOG.debug(" getList key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return (List<V>) redisTemplate.opsForList().range(key, 0, -1);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 根据key值截取对应的list合集
         * @param key    缓存key
         * @param start    开始位置
         * @param end    结束位置
         * @return
         */
        public static void trimList(String key, int start, int end) {
        LOG.debug(" trimList key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return;
            }
            key = buildKey(key);
            redisTemplate.opsForList().trim(key, start, end);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        }
     
        /**
         * 取出list合集中指定位置的对象
         * @param key    缓存key
         * @param index    索引位置
         * @return Object    list指定索引位置的对象
         */
        public static Object getIndexList(String key, int index) {
        LOG.debug(" getIndexList key :{}, index:{}", key, index);
        try {
            if (isClose() || isEmpty(key) || index < 0) {
            return null;
            }
            key = buildKey(key);
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
        /**
         * Object存入List
         * @param <T>
         * @param key    缓存key
         * @param value    List中的值
         * @return    true:成功 
         *         false:失败
         */
        public static boolean addList(String key, Object value) {
        LOG.debug(" addList key :{}, value:{}", key, value);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForList().leftPush(key, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 缓存存入List
         * @param <T>
         * @param key    缓存key
         * @param list    缓存List
         * @param seconds    秒数
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setList(String key, List<T> list, long seconds) {
        //LOG.debug(" setList key :{}, value:{}, seconds:{}", key, list, seconds);
        try {
            if (isClose() || isEmpty(key) || isEmpty(list)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForList().leftPushAll(key, list.toArray());
            if (seconds > 0) {
            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * set集合存入缓存
         * @param <T>
         * @param key    缓存key
         * @param set    缓存set集合
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setSet(String key, Set<T> set) {
        LOG.debug(" setSet key :{}, value:{}", key, set);
        try {
            if (isClose() || isEmpty(key) || isEmpty(set)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForSet().add(key, set.toArray());
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * set集合中增加value
         * @param <T>
         * @param key    缓存key
         * @param value    增加的value
         * @return    true:成功 
         *         false:失败
         */
        public static boolean addSet(String key, Object value) {
        LOG.debug(" addSet key :{}, value:{}", key, value);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForSet().add(key, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * set集合存入缓存
         * @param <T>
         * @param key    缓存key
         * @param set    缓存set集合
         * @param seconds    秒数
         * @return    true:成功 
         *         false:失败
         */
        public static <T> boolean setSet(String key, Set<T> set, long seconds) {
        //LOG.debug(" setSet key :{}, value:{}, seconds:{}", key, set, seconds);
        try {
            if (isClose() || isEmpty(key) || isEmpty(set)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForSet().add(key, set.toArray());
            if (seconds > 0) {
            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 取出缓存中对应的set合集
         * @param <T>
         * @param key    缓存key
         * @return Set<Object> 缓存中的set合集
         */
        public static <T> Set<T> getSet(String key) {
        LOG.debug(" getSet key :{}", key);
        try {
            if (isClose() || isEmpty(key)) {
            return null;
            }
            key = buildKey(key);
            return (Set<T>) redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return null;
        }
     
        /**
         * 有序集合存入数值
         * @param key    缓存key
         * @param value    缓存value
         * @param score    评分
         * @return
         */
        public static boolean addZSet(String key, Object value, double score) {
        //LOG.debug(" addZSet key :{},value:{}, score:{}", key, value, score);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            return redisTemplate.opsForZSet().add(key, value, score);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 从有序集合中删除指定值
         * @param key    缓存key
         * @param value    缓存value
         * @return
         */
        public static boolean removeZSet(String key, Object value) {
        LOG.debug(" removeZSet key :{},value:{}", key, value);
        try {
            if (isClose() || isEmpty(key) || isEmpty(value)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForZSet().remove(key, value);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 从有序集合中删除指定位置的值
         * @param key    缓存key
         * @param start    起始位置
         * @param end    结束为止
         * @return
         */
        public static boolean removeZSet(String key, long start, long end) {
        //LOG.debug(" removeZSet key :{},start:{}, end:{}", key, start, end);
        try {
            if (isClose() || isEmpty(key)) {
            return false;
            }
            key = buildKey(key);
            redisTemplate.opsForZSet().removeRange(key, start, end);
            return true;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
        }
     
        /**
         * 从有序集合中获取指定位置的值
         * @param key    缓存key
         * @param start    起始位置
         * @param end    结束为止
         * @return
         */
        public static <T> Set<T> getZSet(String key, long start, long end) {
        //LOG.debug(" getZSet key :{},start:{}, end:{}", key, start, end);
        try {
            if (isClose() || isEmpty(key)) {
            return Collections.emptySet();
            }
            key = buildKey(key);
            return (Set<T>) redisTemplate.opsForZSet().range(key, start, end);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return Collections.emptySet();
        }
 
        public static void send(String chat1, String msg) {
            redisTemplate.convertAndSend(chat1,msg);
        }
 
        /**
         * 发布消息到指定的频道
         *
         * @param channel
         * @param message
         */
        public static void publish(final String channel, final String message) {
            redisTemplate.execute(new RedisCallback<Object>() {
                public Object doInRedis(final RedisConnection connection)
                        throws DataAccessException {
                    ((Jedis) connection.getNativeConnection()).publish(channel, message);
                    return null;
                }
            });
        }
 
        /**
         * 订阅给定的一个或多个频道的信息
         *
         * @param jedisPubSub
         * @param channels
         */
        public static void subscribe(final JedisPubSub jedisPubSub, final String... channels) {
            redisTemplate.execute(new RedisCallback<Object>() {
                @Override
                public Object doInRedis(RedisConnection connection) throws DataAccessException {
                    ((Jedis) connection.getNativeConnection()).subscribe(jedisPubSub, channels);
                    return null;
                }
            });
        }
 
 
}