【笔记】redis实现类

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
package hry.redis.common.utils.impl;
 
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import hry.redis.common.utils.RedisService;
import hry.redis.common.utils.SerializeUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Tuple;
 
/**
 */
public class RedisServiceImpl implements RedisService {
 
    @Resource(name = "jedisPool")
    private JedisPool jedisPool;
 
    // 锁前缀
    private static final String LOCK_PREFIX = "LOCK:LOCK_";
 
    private static final int timeout = 20;
 
    private static final int time = 10 * 1000;
 
    @Override
    public String save(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public Map<String, String> getMap(String key) {
        Jedis jedis = null;
 
        try {
            jedis = this.jedisPool.getResource();
            Map<String, String> map = jedis.hgetAll(key);
            Map var4 = map;
            return var4;
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            jedis.close();
        }
 
        return null;
    }
 
    @Override
    public String getMap(String key, String code) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String ss = jedis.hget(key, code);
            return ss;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return null;
    }
 
    @Override
    public void hset(String hkey, String key, String value) {
        Jedis jedis = null;
 
        try {
            jedis = this.jedisPool.getResource();
            jedis.hset(hkey,key,value);
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            jedis.close();
        }
 
    }
 
    @Override
    public <T> List<T> getList(String key) {
        Jedis jedis = null;
        try {
 
            jedis = jedisPool.getResource();
            byte[] in = jedis.get(key.getBytes());
            List<T> list = (List<T>) SerializeUtil.unserialize(in);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return null;
    }
 
    @Override
    public <T> void setList(String key, List<T> list) {
 
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), SerializeUtil.serialize(list));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public void saveMap(String key, Map<String, String> map) {
        Jedis jedis = null;
 
        try {
            jedis = this.jedisPool.getResource();
            jedis.hmset(key, map);
        } catch (Exception var8) {
            ;
        } finally {
            jedis.close();
        }
 
    }
 
    @Override
    public void save(String key, String value, int second) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            jedis.expire(key, second);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public Long delete(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            return (long) 0;
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public void setTime(String key, int second) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.expire(key, second);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
 
    /***
     *
     * @param patt
     * @return
     */
    @Override
    public Set<String> keys(String patt) {
 
        Jedis jedis = jedisPool.getResource();
        try {
            Set<String> keys = jedis.keys("*" + patt + "*");
            return keys;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
        return null;
    }
 
    @Override
    public Set<String> hkeys(final String key){
        Jedis jedis = jedisPool.getResource();
        try {
            Set<String> keys = jedis.hkeys(key);
            return keys;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return null;
    }
 
    @Override
    public String getLendConfig(String key) {
 
        String val = "";
        Jedis jedis = jedisPool.getResource();
        try {
            String string = jedis.get("configCache:financeLendConfig");
            JSONObject obj = JSON.parseObject(string);
            if (null != key && obj.get(key) != null) {
                val = obj.get(key).toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return val;
 
    }
 
    @Override
    public String hget(String hkey, String key) {
 
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hget(hkey, key);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            jedis.close();
        }
 
    }
 
    @Override
    public Map<String,String> hgetall(String hkey) {
 
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hgetAll(hkey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
 
    }
 
    /**
     * 获取key生存时间
     */
    @Override
    public Long getKeyTime(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            Long l = jedis.ttl(key);
            return l;
        } catch (Exception e) {
        } finally {
            jedis.close();
        }
        return null;
    }
 
    @Override
    public Set<String> noPerkeys(String patt) {
 
 
        Jedis jedis = jedisPool.getResource();
        try {
            Set<String> keys = jedis.keys(patt + "*");
            return keys;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
        return null;
 
    }
 
    /**
     * 加锁
     *
     * @param key
     * @return
     */
    @Override
    public boolean lock(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String nkey = LOCK_PREFIX + key;
            String value = System.currentTimeMillis() + time + "";
            Long result = jedis.setnx(nkey, value);
            if (result == 1) {
                jedis.expire(nkey, timeout);
                return true;
            } else {
                String dkey = jedis.get(LOCK_PREFIX + key);
                Long skey = dkey != null ? Long.valueOf(dkey) : null;
                long currentTime = System.currentTimeMillis();
                if ((null != skey && skey > currentTime) || skey == null) {
                    String svalue = System.currentTimeMillis() + time + "";
                    Long setnx = jedis.setnx(nkey, svalue);
                    if (setnx == 1) {
                        jedis.expire(nkey, timeout);
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }
        return false;
    }
 
    /**
     * 释放锁
     *
     * @param key
     */
    @Override
    public void unLock(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String nkey = LOCK_PREFIX + key;
 
            jedis.del(nkey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    /**
     * 保存一个单个键值对进到map里.如果mKey已经存在于map里将会覆盖之前的值。
     *
     * @param key
     * @param mKey
     * @param value
     */
    @Override
    public void saveMap(String key, String mKey, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hset(key, mKey, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    /**
     * 删除某个key 所对应的value。
     *
     * @param key
     * @param mKey
     * @return
     */
    @Override
    public String delMapKey(String key, String mKey) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String ss = jedis.hget(key, mKey);
            return ss;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return null;
    }
 
    @Override
    public Long getExpireTime(String key) {
        Long result = -2L;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.ttl(key);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return result;
    }
 
    @Override
    public void rpush(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.rpush(key,value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }
    @Override
    public void zadd(String key,double score, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.zadd(key,score,value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }
 
    @Override
    public void zream(String key, double score) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.zremrangeByScore(key,score,score);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
    }
 
    @Override
    public Set<String> zrange(String key, long start,long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zrange(key,start,end);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    @Override
    public Set<String> zrangeByScore(String key, double min,double max) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return  jedis.zrangeByScore(key, min, max);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    @Override
    public Long setnx(String key, String value){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.setnx(key, value);
            return result;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        return null;
    }
 
    @Override
    public Long expire(String key, int seconds){
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.expire(key, seconds);
            return result;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        return null;
    }
 
 
    @Override
    public void publish(String channel, String message) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.publish(channel,message);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    @Override
    public void subscribe(JedisPubSub jedisPubSub, String channel) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(jedisPubSub,channel);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
 
    /**
     * set sadd
     * @param key
     * @param val
     * @return
     * @author denghf
     */
    public Long sadd(String key, String... val){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.sadd(key, val);
        } catch (Exception e) {
            e.printStackTrace();
            return 0L;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * set smembers
     * @param key
     * @return
     * @author denghf
     */
    public Set<String> smembers(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Set<String> smembers = jedis.smembers(key);
            return smembers;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * zremrangeByScore
     * @param key
     * @param scoreMin
     * @param scoreMax
     * @return
     * @author denghf
     */
    @Override
    public Long zremrangeByScore(String key, double scoreMin, double scoreMax) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long aLong = jedis.zremrangeByScore(key, scoreMin, scoreMax);
            return aLong;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * zscore
     * @param key
     * @param member
     * @return
     * @author denghf
     */
    public Long zrank(String key, String member){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long aLong = jedis.zrank(key, member);
            return aLong;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * zscore
     * @param key
     * @param member
     * @return
     * @author denghf
     */
    public Double zscore(String key, String member){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Double aLong = jedis.zscore(key, member);
            return aLong;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * zrem
     * @param key
     * @param members
     * @return
     * @author denghf
     */
    public Long zrem(String key, String... members){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long aLong = jedis.zrem(key, members);
            return aLong;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * zrangeWithScores
     * @param key
     * @param start
     * @param end
     * @return
     */
    @Override
    public Set<Tuple> zrangeWithScores(String key, long start,long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Set<Tuple> tuples = jedis.zrangeWithScores(key, start, end);
            return tuples;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * lpop
     * @param key
     * @return
     * @author denghf
     */
    public String lpop(String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String lpop = jedis.lpop(key);
            return lpop;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * lrange
     * @param key
     * @return
     * @author denghf
     */
    public List<String> lrange(String key, long start, long end){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            List<String> lrange = jedis.lrange(key, start, end);
            return lrange;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * lrem
     * @param key
     * @return
     * @author denghf
     */
    public Long lrem(String key, long count, String value){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long lrem = jedis.lrem(key, count, value);
            return lrem;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * hdel
     * @param key
     * @return
     * @author denghf
     */
    public Long hdel(String key, String... field){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long hdel = jedis.hdel(key, field);
            return hdel;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * lpush
     * @param key
     * @return
     * @author denghf
     */
    public Long lpush(String key, String... val){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Long lpush = jedis.lpush(key, val);
            return lpush;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * set
     * @param key
     * @param val
     * @author denghf
     * @return
     */
    public String set(byte[] key, byte[] val){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String set = jedis.set(key, val);
            return set;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    /**
     * get
     * @param key
     * @author denghf
     * @return
     */
    public byte[] get(byte[] key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
    @Override
    public Set<String> delkeys(String patt) {
 
        Jedis jedis = jedisPool.getResource();
        try {
            Set<String> keys = jedis.keys("*" + patt + "*");
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()) {
                jedis.del(iterator.next());
            }
 
            return keys;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
 
        return null;
    }
 
    @Override
    public Set<String> zrevrange(String key, long start,long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zrevrange(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
 
    @Override
    public Set<String> zrevrangeByScore(String key, String start,String end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zrevrangeByScore(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis != null) {
                jedis.close();
            }
        }
        return null;
    }
}

  

posted @   我的bug  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示