yangys
2024-05-30 a3686cfa49bf53fb91a2ceb960cf15b3ebdac641
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
package com.qianwen.core.redis.cache;
 
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import com.qianwen.core.tool.utils.CollectionUtil;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
 
public class BladeRedis {
    private final RedisTemplate<String, Object> redisTemplate;
    private final ValueOperations<String, Object> valueOps;
    private final HashOperations<String, Object, Object> hashOps;
    private final ListOperations<String, Object> listOps;
    private final SetOperations<String, Object> setOps;
    private final ZSetOperations<String, Object> zSetOps;
 
    public RedisTemplate<String, Object> getRedisTemplate() {
        return this.redisTemplate;
    }
 
    public ValueOperations<String, Object> getValueOps() {
        return this.valueOps;
    }
 
    public HashOperations<String, Object, Object> getHashOps() {
        return this.hashOps;
    }
 
    public ListOperations<String, Object> getListOps() {
        return this.listOps;
    }
 
    public SetOperations<String, Object> getSetOps() {
        return this.setOps;
    }
 
    public ZSetOperations<String, Object> getZSetOps() {
        return this.zSetOps;
    }
 
    public BladeRedis(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
        Assert.notNull(redisTemplate, "redisTemplate is null");
        this.valueOps = redisTemplate.opsForValue();
        this.hashOps = redisTemplate.opsForHash();
        this.listOps = redisTemplate.opsForList();
        this.setOps = redisTemplate.opsForSet();
        this.zSetOps = redisTemplate.opsForZSet();
    }
 
    public void set(CacheKey cacheKey, Object value) {
        String key = cacheKey.getKey();
        Duration expire = cacheKey.getExpire();
        if (expire == null) {
            set(key, value);
        } else {
            setEx(key, value, expire);
        }
    }
 
    public void set(String key, Object value) {
        this.valueOps.set(key, value);
    }
 
    public void setEx(String key, Object value, Duration timeout) {
        this.valueOps.set(key, value, timeout);
    }
 
    public void setEx(String key, Object value, Long seconds) {
        this.valueOps.set(key, value, seconds.longValue(), TimeUnit.SECONDS);
    }
 
    @Nullable
    public <T> T get(String key) {
        return (T) this.valueOps.get(key);
    }
 
    @Nullable
    public <T> T get(String key, Supplier<T> loader) {
        T value = (T) get(key);
        if (value != null) {
            return value;
        }
        T value2 = loader.get();
        if (value2 == null) {
            return null;
        }
        set(key, value2);
        return value2;
    }
 
    @Nullable
    public <T> T get(CacheKey cacheKey) {
        return (T) this.valueOps.get(cacheKey.getKey());
    }
 
    @Nullable
    public <T> T get(CacheKey cacheKey, Supplier<T> loader) {
        String key = cacheKey.getKey();
        T value = (T) get(key);
        if (value != null) {
            return value;
        }
        T value2 = loader.get();
        if (value2 == null) {
            return null;
        }
        set(cacheKey, value2);
        return value2;
    }
 
    public Boolean del(String key) {
        return this.redisTemplate.delete(key);
    }
 
    public Boolean del(CacheKey key) {
        return this.redisTemplate.delete(key.getKey());
    }
 
    public Long del(String... keys) {
        return del(Arrays.asList(keys));
    }
 
    public Long del(Collection<String> keys) {
        return this.redisTemplate.delete(keys);
    }
 
    public Set<String> keys(String pattern) {
        return this.redisTemplate.keys(pattern);
    }
 
    public void mSet(Object... keysValues) {
        this.valueOps.multiSet(CollectionUtil.toMap(keysValues));
    }
 
    public List<Object> mGet(String... keys) {
        return mGet(Arrays.asList(keys));
    }
 
    public List<Object> mGet(Collection<String> keys) {
        return this.valueOps.multiGet(keys);
    }
 
    public Long decr(String key) {
        return this.valueOps.decrement(key);
    }
 
    public Long decrBy(String key, long longValue) {
        return this.valueOps.decrement(key, longValue);
    }
 
    public Long incr(String key) {
        return this.valueOps.increment(key);
    }
 
    public Long incrBy(String key, long longValue) {
        return this.valueOps.increment(key, longValue);
    }
 
    public Long getCounter(String key) {
        return Long.valueOf(String.valueOf(this.valueOps.get(key)));
    }
 
    public Boolean exists(String key) {
        return this.redisTemplate.hasKey(key);
    }
 
    public String randomKey() {
        return (String) this.redisTemplate.randomKey();
    }
 
    public void rename(String oldkey, String newkey) {
        this.redisTemplate.rename(oldkey, newkey);
    }
 
    public Boolean move(String key, int dbIndex) {
        return this.redisTemplate.move(key, dbIndex);
    }
 
    /**
     * 设置指定key的超市时间
     * @param key redisKey
     * @param seconds 秒数
     * @return
     */
    public Boolean expire(String key, long seconds) {
        return this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }
 
    public Boolean expire(String key, Duration timeout) {
        return expire(key, timeout.getSeconds());
    }
 
    public Boolean expireAt(String key, Date date) {
        return this.redisTemplate.expireAt(key, date);
    }
 
    public Boolean expireAt(String key, long unixTime) {
        return expireAt(key, new Date(unixTime));
    }
 
    public Boolean pexpire(String key, long milliseconds) {
        return this.redisTemplate.expire(key, milliseconds, TimeUnit.MILLISECONDS);
    }
 
    public <T> T getSet(String key, Object value) {
        return (T) this.valueOps.getAndSet(key, value);
    }
 
    public Boolean persist(String key) {
        return this.redisTemplate.persist(key);
    }
 
    public String type(String key) {
        return this.redisTemplate.type(key).code();
    }
 
    public Long ttl(String key) {
        return this.redisTemplate.getExpire(key);
    }
 
    public Long pttl(String key) {
        return this.redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
    }
 
    public void hSet(String key, Object field, Object value) {
        this.hashOps.put(key, field, value);
    }
 
    public void hMset(String key, Map<Object, Object> hash) {
        this.hashOps.putAll(key, hash);
    }
 
    public <T> T hGet(String key, Object field) {
        return (T) this.hashOps.get(key, field);
    }
 
    public List hmGet(String key, Object... fields) {
        return hmGet(key, Arrays.asList(fields));
    }
 
    public List hmGet(String key, Collection<Object> hashKeys) {
        return this.hashOps.multiGet(key, hashKeys);
    }
 
    public Long hDel(String key, Object... fields) {
        return this.hashOps.delete(key, fields);
    }
 
    public Boolean hExists(String key, Object field) {
        return this.hashOps.hasKey(key, field);
    }
 
    public Map hGetAll(String key) {
        return this.hashOps.entries(key);
    }
 
    public List hVals(String key) {
        return this.hashOps.values(key);
    }
 
    public Set<Object> hKeys(String key) {
        return this.hashOps.keys(key);
    }
 
    public Long hLen(String key) {
        return this.hashOps.size(key);
    }
 
    public Long hIncrBy(String key, Object field, long value) {
        return this.hashOps.increment(key, field, value);
    }
 
    public Double hIncrByFloat(String key, Object field, double value) {
        return this.hashOps.increment(key, field, value);
    }
 
    public <T> T lIndex(String key, long index) {
        return (T) this.listOps.index(key, index);
    }
 
    public Long lLen(String key) {
        return this.listOps.size(key);
    }
 
    public <T> T lPop(String key) {
        return (T) this.listOps.leftPop(key);
    }
 
    public Long lPush(String key, Object... values) {
        return this.listOps.leftPush(key, values);
    }
 
    public void lSet(String key, long index, Object value) {
        this.listOps.set(key, index, value);
    }
 
    public Long lRem(String key, long count, Object value) {
        return this.listOps.remove(key, count, value);
    }
 
    public List lRange(String key, long start, long end) {
        return this.listOps.range(key, start, end);
    }
 
    public void lTrim(String key, long start, long end) {
        this.listOps.trim(key, start, end);
    }
 
    public <T> T rPop(String key) {
        return (T) this.listOps.rightPop(key);
    }
 
    public Long rPush(String key, Object... values) {
        return this.listOps.rightPush(key, values);
    }
 
    public <T> T rPopLPush(String srcKey, String dstKey) {
        return (T) this.listOps.rightPopAndLeftPush(srcKey, dstKey);
    }
 
    public Long sAdd(String key, Object... members) {
        return this.setOps.add(key, members);
    }
 
    public <T> T sPop(String key) {
        return (T) this.setOps.pop(key);
    }
 
    public Set sMembers(String key) {
        return this.setOps.members(key);
    }
 
    public boolean sIsMember(String key, Object member) {
        return this.setOps.isMember(key, member).booleanValue();
    }
 
    public Set sInter(String key, String otherKey) {
        return this.setOps.intersect(key, otherKey);
    }
 
    public Set sInter(String key, Collection<String> otherKeys) {
        return this.setOps.intersect(key, otherKeys);
    }
 
    public <T> T sRandMember(String key) {
        return (T) this.setOps.randomMember(key);
    }
 
    public List sRandMember(String key, int count) {
        return this.setOps.randomMembers(key, count);
    }
 
    public Long sRem(String key, Object... members) {
        return this.setOps.remove(key, members);
    }
 
    public Set sUnion(String key, String otherKey) {
        return this.setOps.union(key, otherKey);
    }
 
    public Set sUnion(String key, Collection<String> otherKeys) {
        return this.setOps.union(key, otherKeys);
    }
 
    public Set sDiff(String key, String otherKey) {
        return this.setOps.difference(key, otherKey);
    }
 
    public Set sDiff(String key, Collection<String> otherKeys) {
        return this.setOps.difference(key, otherKeys);
    }
 
    public Boolean zAdd(String key, Object member, double score) {
        return this.zSetOps.add(key, member, score);
    }
 
    public Long zAdd(String key, Map<Object, Double> scoreMembers) {
        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();
        scoreMembers.forEach((k, v) -> tuples.add(new DefaultTypedTuple(k, v)));
        return this.zSetOps.add(key, tuples);
    }
 
    public Long zCard(String key) {
        return this.zSetOps.zCard(key);
    }
 
    public Long zCount(String key, double min, double max) {
        return this.zSetOps.count(key, min, max);
    }
 
    public Double zIncrBy(String key, Object member, double score) {
        return this.zSetOps.incrementScore(key, member, score);
    }
 
    public Set zRange(String key, long start, long end) {
        return this.zSetOps.range(key, start, end);
    }
 
    public Set zRevrange(String key, long start, long end) {
        return this.zSetOps.reverseRange(key, start, end);
    }
 
    public Set zRangeByScore(String key, double min, double max) {
        return this.zSetOps.rangeByScore(key, min, max);
    }
 
    public Long zRank(String key, Object member) {
        return this.zSetOps.rank(key, member);
    }
 
    public Long zRevrank(String key, Object member) {
        return this.zSetOps.reverseRank(key, member);
    }
 
    public Long zRem(String key, Object... members) {
        return this.zSetOps.remove(key, members);
    }
 
    public Double zScore(String key, Object member) {
        return this.zSetOps.score(key, member);
    }
}