中国算力平台算力登记系统2.0
yanzhaofeige
3 days ago 9a224d216652ef2d6ff2e0bd01fba69c86a3f277
commit | author | age
43dc29 1 package com.odcc.cpzidc.common.core.redis;
Y 2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.concurrent.TimeUnit;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.data.redis.core.BoundSetOperations;
11 import org.springframework.data.redis.core.HashOperations;
12 import org.springframework.data.redis.core.RedisTemplate;
13 import org.springframework.data.redis.core.ValueOperations;
14 import org.springframework.stereotype.Component;
15
16 /**
17  * spring redis 工具类
18  *
19  * @author ruoyi
20  **/
21 @SuppressWarnings(value = { "unchecked", "rawtypes" })
22 @Component
23 public class RedisCache
24 {
25     @Autowired
26     public RedisTemplate redisTemplate;
27
28     /**
29      * 缓存基本的对象,Integer、String、实体类等
30      *
31      * @param key 缓存的键值
32      * @param value 缓存的值
33      */
34     public <T> void setCacheObject(final String key, final T value)
35     {
36         redisTemplate.opsForValue().set(key, value);
37     }
38
39     /**
40      * 缓存基本的对象,Integer、String、实体类等
41      *
42      * @param key 缓存的键值
43      * @param value 缓存的值
44      * @param timeout 时间
45      * @param timeUnit 时间颗粒度
46      */
47     public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
48     {
49         redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
50     }
51
52     /**
53      * 设置有效时间
54      *
55      * @param key Redis键
56      * @param timeout 超时时间
57      * @return true=设置成功;false=设置失败
58      */
59     public boolean expire(final String key, final long timeout)
60     {
61         return expire(key, timeout, TimeUnit.SECONDS);
62     }
63
64     /**
65      * 设置有效时间
66      *
67      * @param key Redis键
68      * @param timeout 超时时间
69      * @param unit 时间单位
70      * @return true=设置成功;false=设置失败
71      */
72     public boolean expire(final String key, final long timeout, final TimeUnit unit)
73     {
74         return redisTemplate.expire(key, timeout, unit);
75     }
76
77     /**
78      * 获取有效时间
79      *
80      * @param key Redis键
81      * @return 有效时间
82      */
83     public long getExpire(final String key)
84     {
85         return redisTemplate.getExpire(key);
86     }
87
88     /**
89      * 判断 key是否存在
90      *
91      * @param key 键
92      * @return true 存在 false不存在
93      */
94     public Boolean hasKey(String key)
95     {
96         return redisTemplate.hasKey(key);
97     }
98
99     /**
100      * 获得缓存的基本对象。
101      *
102      * @param key 缓存键值
103      * @return 缓存键值对应的数据
104      */
105     public <T> T getCacheObject(final String key)
106     {
107         ValueOperations<String, T> operation = redisTemplate.opsForValue();
108         return operation.get(key);
109     }
110
111     /**
112      * 删除单个对象
113      *
114      * @param key
115      */
116     public boolean deleteObject(final String key)
117     {
118         return redisTemplate.delete(key);
119     }
120
121     /**
122      * 删除集合对象
123      *
124      * @param collection 多个对象
125      * @return
126      */
127     public boolean deleteObject(final Collection collection)
128     {
129         return redisTemplate.delete(collection) > 0;
130     }
131
132     /**
133      * 缓存List数据
134      *
135      * @param key 缓存的键值
136      * @param dataList 待缓存的List数据
137      * @return 缓存的对象
138      */
139     public <T> long setCacheList(final String key, final List<T> dataList)
140     {
141         Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
142         return count == null ? 0 : count;
143     }
144
145     /**
146      * 获得缓存的list对象
147      *
148      * @param key 缓存的键值
149      * @return 缓存键值对应的数据
150      */
151     public <T> List<T> getCacheList(final String key)
152     {
153         return redisTemplate.opsForList().range(key, 0, -1);
154     }
155
156     /**
157      * 缓存Set
158      *
159      * @param key 缓存键值
160      * @param dataSet 缓存的数据
161      * @return 缓存数据的对象
162      */
163     public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
164     {
165         BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
166         Iterator<T> it = dataSet.iterator();
167         while (it.hasNext())
168         {
169             setOperation.add(it.next());
170         }
171         return setOperation;
172     }
173
174     /**
175      * 获得缓存的set
176      *
177      * @param key
178      * @return
179      */
180     public <T> Set<T> getCacheSet(final String key)
181     {
182         return redisTemplate.opsForSet().members(key);
183     }
184
185     /**
186      * 缓存Map
187      *
188      * @param key
189      * @param dataMap
190      */
191     public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
192     {
193         if (dataMap != null) {
194             redisTemplate.opsForHash().putAll(key, dataMap);
195         }
196     }
197
198     /**
199      * 获得缓存的Map
200      *
201      * @param key
202      * @return
203      */
204     public <T> Map<String, T> getCacheMap(final String key)
205     {
206         return redisTemplate.opsForHash().entries(key);
207     }
208
209     /**
210      * 往Hash中存入数据
211      *
212      * @param key Redis键
213      * @param hKey Hash键
214      * @param value 值
215      */
216     public <T> void setCacheMapValue(final String key, final String hKey, final T value)
217     {
218         redisTemplate.opsForHash().put(key, hKey, value);
219     }
220
221     /**
222      * 获取Hash中的数据
223      *
224      * @param key Redis键
225      * @param hKey Hash键
226      * @return Hash中的对象
227      */
228     public <T> T getCacheMapValue(final String key, final String hKey)
229     {
230         HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
231         return opsForHash.get(key, hKey);
232     }
233
234     /**
235      * 获取多个Hash中的数据
236      *
237      * @param key Redis键
238      * @param hKeys Hash键集合
239      * @return Hash对象集合
240      */
241     public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
242     {
243         return redisTemplate.opsForHash().multiGet(key, hKeys);
244     }
245
246     /**
247      * 删除Hash中的某条数据
248      *
249      * @param key Redis键
250      * @param hKey Hash键
251      * @return 是否成功
252      */
253     public boolean deleteCacheMapValue(final String key, final String hKey)
254     {
255         return redisTemplate.opsForHash().delete(key, hKey) > 0;
256     }
257
258     /**
259      * 获得缓存的基本对象列表
260      *
261      * @param pattern 字符串前缀
262      * @return 对象列表
263      */
264     public Collection<String> keys(final String pattern)
265     {
266         return redisTemplate.keys(pattern);
267     }
268 }