提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.common.utils.redis; |
X |
2 |
|
|
3 |
import com.dl.common.utils.spring.SpringUtils; |
|
4 |
import lombok.AccessLevel; |
|
5 |
import lombok.NoArgsConstructor; |
|
6 |
import org.redisson.api.RMap; |
|
7 |
import org.springframework.cache.Cache; |
|
8 |
import org.springframework.cache.CacheManager; |
|
9 |
|
|
10 |
import java.util.Set; |
|
11 |
|
|
12 |
/** |
|
13 |
* 缓存操作工具类 {@link } |
|
14 |
* |
|
15 |
* @author Michelle.Chung |
|
16 |
* @date 2022/8/13 |
|
17 |
*/ |
|
18 |
@NoArgsConstructor(access = AccessLevel.PRIVATE) |
|
19 |
@SuppressWarnings(value = {"unchecked"}) |
|
20 |
public class CacheUtils { |
|
21 |
|
|
22 |
private static final CacheManager CACHE_MANAGER = SpringUtils.getBean(CacheManager.class); |
|
23 |
|
|
24 |
/** |
|
25 |
* 获取缓存组内所有的KEY |
|
26 |
* |
|
27 |
* @param cacheNames 缓存组名称 |
|
28 |
*/ |
|
29 |
public static Set<Object> keys(String cacheNames) { |
|
30 |
RMap<Object, Object> rmap = (RMap<Object, Object>) CACHE_MANAGER.getCache(cacheNames).getNativeCache(); |
|
31 |
return rmap.keySet(); |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* 获取缓存值 |
|
36 |
* |
|
37 |
* @param cacheNames 缓存组名称 |
|
38 |
* @param key 缓存key |
|
39 |
*/ |
|
40 |
public static <T> T get(String cacheNames, Object key) { |
|
41 |
Cache.ValueWrapper wrapper = CACHE_MANAGER.getCache(cacheNames).get(key); |
|
42 |
return wrapper != null ? (T) wrapper.get() : null; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 保存缓存值 |
|
47 |
* |
|
48 |
* @param cacheNames 缓存组名称 |
|
49 |
* @param key 缓存key |
|
50 |
* @param value 缓存值 |
|
51 |
*/ |
|
52 |
public static void put(String cacheNames, Object key, Object value) { |
|
53 |
CACHE_MANAGER.getCache(cacheNames).put(key, value); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* 删除缓存值 |
|
58 |
* |
|
59 |
* @param cacheNames 缓存组名称 |
|
60 |
* @param key 缓存key |
|
61 |
*/ |
|
62 |
public static void evict(String cacheNames, Object key) { |
|
63 |
CACHE_MANAGER.getCache(cacheNames).evict(key); |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* 清空缓存值 |
|
68 |
* |
|
69 |
* @param cacheNames 缓存组名称 |
|
70 |
*/ |
|
71 |
public static void clear(String cacheNames) { |
|
72 |
CACHE_MANAGER.getCache(cacheNames).clear(); |
|
73 |
} |
|
74 |
|
|
75 |
} |