Helius
2021-06-23 b8ca59259b0690f664ee70ecca69dfb53700c653
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
package com.xzx.gc.common.utils;
 
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
/**
 * @author: zhongzan
 * @create: 2019-06-24 16:57
 * @description: redis工具类
 */
@Component
public class RedisUtil {
 
    @Autowired
    private RedisTemplate redisTemplate;
 
 
 
    /**
     * Remove the specified keys. If a given key does not exist no operation is
     * performed for this key.
     *
     * return false if one of the key is not exist.
     */
    public Long del(final String... keys) {
        return redisTemplate.delete(Arrays.asList(keys));
    }
    /**
     * 删除对应的value
     *
     * @param key
     */
    public Boolean remove(final String key) {
        if (exists(key)) {
            return redisTemplate.delete(key);
        }
        return Boolean.TRUE;
    }
 
    // / String Actions ///
 
    /**
     * Get the value of the specified key. If the key does not exist null is
     * returned. If the value stored at key is not a string an error is returned
     * because GET can only handle string values.
     */
    public String get(final String key) {
        Object result = redisTemplate.opsForValue().get(key);
        return null == result ? null : result.toString();
    }
 
    public <T>T get(final String key, Class<T> type) {
        return  JSONObject.parseObject(get(key), type);
    }
 
    /**
     * Get the value of the specified key as Long.If the key does not exist null is returned.
     */
    public Long getAsLong(final String key) {
        String result = get(key);
        return result != null ? Long.valueOf(result) : null;
    }
 
    /**
     * Get the value of the specified key as Integer.If the key does not exist null is returned.
     */
    public Integer getAsInt(final String key) {
        String result = get(key);
        return result != null ? Integer.valueOf(result) : null;
    }
    public Map<String, Object> getMapValue(String mapName) {
        HashOperations<String, String, Object> hps = redisTemplate.opsForHash();
        return hps.entries(mapName);
    }
 
 
    /**
     * Get the values of all the specified keys. If one or more keys dont exist
     * or is not of type String, a 'nil' value is returned instead of the value
     * of the specified key, but the operation never fails.
     */
    public List<String> mget(final String... keys) {
        return redisTemplate.opsForValue().multiGet(Arrays.asList(keys));
 
    }
 
    /**
     * Set the string value as value of the key.
     * The string can't be longer than 1073741824 bytes (1 GB).
     */
    public void set(final String key, final String value) {
        redisTemplate.opsForValue().set(key, value);
    }
 
    public void set(final String key, final Object value) {
        set(key, JSONObject.toJSONString(value));
    }
    /**
     * The command is exactly equivalent to the following group of commands:
     * The operation is atomic.
     */
    public void setex(final String key, final String value, final int seconds) {
        redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }
 
    /**
     * SETNX works exactly like
     * difference that if the key already exists no operation is performed.
     * SETNX actually means "SET if Not eXists".
     *
     * return true if the key was set.
     */
    public Boolean setnx(final String key, final String value) {
        return redisTemplate.opsForValue().setIfAbsent(key,value);
    }
 
    /**
     * The command is exactly equivalent to the following group of commands: {@link #setex(String, String, int) SETEX} +
     *
     * The operation is atomic.
     */
    public Boolean setnxex(final String key, final String value, final int seconds) {
        return redisTemplate.opsForValue().setIfAbsent(key,value,seconds,TimeUnit.SECONDS);
    }
 
    /**
     * GETSET is an atomic set this value and return the old value command. Set
     * key to the string value and return the old value stored at key. The
     * string can't be longer than 1073741824 bytes (1 GB).
     */
    public String getSet(final String key, final String value) {
        Object result =  redisTemplate.opsForValue().getAndSet(key,value);
        return null == result ? null : result.toString();
    }
 
    /**
     * Increment the number stored at key by one. If the key does not exist or
     * contains a value of a wrong type, set the key to the value of "0" before
     * to perform the increment operation.
     * <p>
     * INCR commands are limited to 64 bit signed integers.
     * <p>
     * Note: this is actually a string operation, that is, in Redis there are not "integer" types. Simply the string
     * stored at the key is parsed as a base 10 64 bit signed integer, incremented, and then converted back as a string.
     *
     * @return Integer reply, this commands will reply with the new value of key
     *         after the increment.
     */
 
    public Long incrBy(final String key, final long increment) {
        return redisTemplate.opsForValue().increment(key,increment);
    }
 
    public Double incrByFloat(final String key, final double increment) {
        return  redisTemplate.opsForValue().increment(key,increment);
    }
 
 
    // / Hash Actions ///
    /**
     * If key holds a hash, retrieve the value associated to the specified
     * field.
     * <p>
     * If the field is not found or the key does not exist, a special 'nil' value is returned.
     */
    public String hget(final String key, final String fieldName) {
        Object result = redisTemplate.opsForHash().get(key,fieldName);
        return null == result ? null : result.toString();
    }
 
    public List<String> hmget(final String key, final String... fieldsNames) {
        return redisTemplate.opsForHash().multiGet(key,Arrays.asList(fieldsNames));
    }
 
    public Map<String, String> hgetAll(final String key) {
        return redisTemplate.opsForHash().entries(key);
    }
 
    public void hset(final String key, final String fieldName, final String value) {
        redisTemplate.opsForHash().put(key,fieldName,value);
    }
 
    public void hmset(final String key, final Map<String, String> map) {
        redisTemplate.opsForHash().putAll(key,map);
 
    }
 
    public Boolean hsetnx(final String key, final String fieldName, final String value) {
        return redisTemplate.opsForHash().putIfAbsent(key,fieldName,value);
    }
 
    public Long hincrBy(final String key, final String fieldName, final long increment) {
        return redisTemplate.opsForHash().increment(key,fieldName,increment);
    }
 
    public Double hincrByFloat(final String key, final String fieldName, final double increment) {
        return redisTemplate.opsForHash().increment(key,fieldName,increment);
    }
 
    public Long hdel(final String key, final String... fieldsNames) {
        return redisTemplate.opsForHash().delete(key,fieldsNames);
    }
 
    public Boolean hexists(final String key, final String fieldName) {
        return redisTemplate.opsForHash().hasKey(key,fieldName);
 
    }
 
    public Boolean exists( String key) {
        return redisTemplate.hasKey(key);
    }
 
    /**
     * 设置过期时间
     * @param key
     * @param timeOut 秒
     * @return
     */
    public Boolean expire( String key,long timeOut) {
        return redisTemplate.expire(key,timeOut,TimeUnit.SECONDS);
    }
 
    public Set<String> hkeys(final String key) {
        return  redisTemplate.opsForHash().keys(key);
    }
 
    public Long hlen(final String key) {
        if (exists(key)){
            return Long.valueOf(redisTemplate.opsForHash().keys(key).size());
        }
        return 0L;
 
    }
 
    // / List Actions ///
 
    public Long lpush(final String key, final String... values) {
        return  redisTemplate.opsForList().leftPushAll(key,values);
    }
 
    public Long rpush(final String key, final String... values) {
        return  redisTemplate.opsForList().rightPushAll(key,values);
    }
 
    public String rpop(final String key) {
        Object result =  redisTemplate.opsForList().rightPop(key);
        return null == result ? null : result.toString();
    }
 
    public String lpop(final String key) {
        Object result =  redisTemplate.opsForList().leftPop(key);
        return null == result ? null : result.toString();
    }
 
    public String brpop(final String key) {
        return brpop(0,key);
    }
 
    public String brpop(final int timeout, final String key) {
        Object result= redisTemplate.opsForList().rightPop(key,timeout,TimeUnit.SECONDS);
        return null == result ? null : result.toString();
    }
 
    /**
     * Not support for sharding.
     */
    public String rpoplpush(final String sourceKey, final String destinationKey) {
        Object result=  redisTemplate.opsForList().rightPopAndLeftPush(sourceKey,destinationKey);
        return null == result ? null : result.toString();
    }
 
    /**
     * Not support for sharding.
     */
    public String brpoplpush(final String source, final String destination, final int timeout) {
        Object result=  redisTemplate.opsForList().rightPopAndLeftPush(source,destination,timeout,TimeUnit.SECONDS);
        return null == result ? null : result.toString();
    }
 
    public Long llen(final String key) {
        return redisTemplate.opsForList().size(key);
    }
 
    public String lindex(final String key, final long index) {
        Object result=  redisTemplate.opsForList().index(key,index);
        return null == result ? null : result.toString();
    }
 
    public Long linsertAfter(final String key, final String pivot,
                             final String value) {
        return redisTemplate.opsForList().rightPush(key,pivot,value);
    }
    public Long linsertBefore(final String key, final String pivot,
                              final String value) {
        return redisTemplate.opsForList().leftPush(key,pivot,value);
    }
    public List<String> lrange(final String key, final int start, final int end) {
        return redisTemplate.opsForList().range(key,start,end);
    }
 
    public void ltrim(final String key, final int start, final int end) {
        redisTemplate.opsForList().trim(key,start,end);
    }
 
 
    public void ltrimFromLeft(final String key, final int size) {
        ltrim(key,0,size-1);
    }
 
    /**
     * 通过索引来设置元素的值。
     * @param key
     * @param index
     * @param value
     * @return
     */
    public void lset(final String key,final long index,final String value) {
        redisTemplate.opsForList().set(key,index,value);
    }
 
    public Boolean lremFirst(final String key, final String value) {
        return lremove(key,1,value);
    }
    public Boolean lremove(final String key, final long count, final String value){
        long l = redisTemplate.opsForList().remove(key,count,value);
        return l>0?Boolean.TRUE:Boolean.FALSE;
    }
    public Boolean lremAll(final String key, final String value) {
        return lremove(key,0,value);
    }
 
    // / Set Actions ///
    public Boolean sadd(final String key, final String member) {
        return redisTemplate.opsForSet().add(key,member)==1? true : false;
    }
 
    // / Set Actions ///
    public Boolean srem(final String key, final String member) {
        return redisTemplate.opsForSet().remove(key,member) == 1 ? true : false;
    }
 
    public Set<String> smembers(final String key) {
        return redisTemplate.opsForSet().members(key);
    }
 
 
    // / Ordered Set Actions ///
    /**
     * return true for add new element, false for only update the score.
     */
    public Boolean zadd(final String key, final double score, final String member) {
        return  redisTemplate.opsForZSet().add(key,member,score);
    }
 
    public Double zscore(final String key, final String member) {
        return redisTemplate.opsForZSet().score(key,member);
    }
 
    public Long zrank(final String key, final String member) {
        return redisTemplate.opsForZSet().rank(key,member);
    }
 
    public Long zrevrank(final String key, final String member) {
        return redisTemplate.opsForZSet().reverseRank(key,member);
    }
 
    public Long zcount(final String key, final double min, final double max) {
        return redisTemplate.opsForZSet().count(key,min,max);
    }
 
    public Set<String> zrange(final String key, final int start, final int end) {
        return redisTemplate.opsForZSet().rangeByScore(key,start,end);
    }
 
    public Set<RedisZSetCommands.Tuple> zrangeWithScores(final String key, final int start, final int end) {
        return redisTemplate.opsForZSet().rangeWithScores(key,start,end);
    }
 
    public Set<String> zrevrange(final String key, final int start, final int end) {
        return redisTemplate.opsForZSet().reverseRange(key,start,end);
 
    }
 
    public Set<RedisZSetCommands.Tuple> zrevrangeWithScores(final String key, final int start, final int end) {
        return redisTemplate.opsForZSet().reverseRangeWithScores(key,start,end);
    }
 
    public Set<String> zrangeByScore(final String key, final double min, final double max) {
        return redisTemplate.opsForZSet().rangeByScore(key,min,max);
 
    }
 
    public Set<RedisZSetCommands.Tuple> zrangeByScoreWithScores(final String key, final double min, final double max) {
        return redisTemplate.opsForZSet().rangeByScoreWithScores(key,min,max);
 
    }
 
    public Set<String> zrevrangeByScore(final String key, final double max, final double min) {
        return redisTemplate.opsForZSet().reverseRangeByScore(key,min,max);
    }
 
    public Set<RedisZSetCommands.Tuple> zrevrangeByScoreWithScores(final String key, final double max, final double min) {
        return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key,min,max);
 
    }
 
    public Boolean zrem(final String key, final String member) {
        return redisTemplate.opsForZSet().remove(key,member) == 1 ? true : false;
    }
 
    public Long zremByScore(final String key, final double start, final double end) {
        return redisTemplate.opsForZSet().removeRangeByScore(key,start,end);
 
    }
 
    public Long zremByRank(final String key, final long start, final long end) {
        return redisTemplate.opsForZSet().removeRange(key,start,end);
    }
 
    public Long zcard(final String key) {
        return  redisTemplate.opsForZSet().zCard(key);
    }
 
    public boolean preventManyCommit(String key, String value, Long expireTime) {
        if(get(key) == null){
            try {
                ValueOperations<String, Object> operations = redisTemplate.opsForValue();
                operations.set(key, value);
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }else{
            return false;
        }
    }
}