|
|
@@ -1,17 +1,12 @@
|
|
1
|
1
|
package com.ruoyi.common.redis.service;
|
|
2
|
2
|
|
|
3
|
|
-import java.util.ArrayList;
|
|
4
|
3
|
import java.util.Collection;
|
|
5
|
|
-import java.util.HashSet;
|
|
6
|
|
-import java.util.Iterator;
|
|
7
|
4
|
import java.util.List;
|
|
8
|
5
|
import java.util.Map;
|
|
9
|
6
|
import java.util.Set;
|
|
10
|
7
|
import java.util.concurrent.TimeUnit;
|
|
11
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
12
|
|
-import org.springframework.data.redis.core.BoundSetOperations;
|
|
13
|
9
|
import org.springframework.data.redis.core.HashOperations;
|
|
14
|
|
-import org.springframework.data.redis.core.ListOperations;
|
|
15
|
10
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
16
|
11
|
import org.springframework.data.redis.core.ValueOperations;
|
|
17
|
12
|
import org.springframework.stereotype.Component;
|
|
|
@@ -33,13 +28,10 @@ public class RedisService
|
|
33
|
28
|
*
|
|
34
|
29
|
* @param key 缓存的键值
|
|
35
|
30
|
* @param value 缓存的值
|
|
36
|
|
- * @return 缓存的对象
|
|
37
|
31
|
*/
|
|
38
|
|
- public <T> ValueOperations<String, T> setCacheObject(String key, T value)
|
|
|
32
|
+ public <T> void setCacheObject(final String key, final T value)
|
|
39
|
33
|
{
|
|
40
|
|
- ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
|
41
|
|
- operation.set(key, value);
|
|
42
|
|
- return operation;
|
|
|
34
|
+ redisTemplate.opsForValue().set(key, value);
|
|
43
|
35
|
}
|
|
44
|
36
|
|
|
45
|
37
|
/**
|
|
|
@@ -49,13 +41,35 @@ public class RedisService
|
|
49
|
41
|
* @param value 缓存的值
|
|
50
|
42
|
* @param timeout 时间
|
|
51
|
43
|
* @param timeUnit 时间颗粒度
|
|
52
|
|
- * @return 缓存的对象
|
|
53
|
44
|
*/
|
|
54
|
|
- public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit)
|
|
|
45
|
+ public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
|
|
55
|
46
|
{
|
|
56
|
|
- ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
|
57
|
|
- operation.set(key, value, timeout, timeUnit);
|
|
58
|
|
- return operation;
|
|
|
47
|
+ redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ /**
|
|
|
51
|
+ * 设置有效时间
|
|
|
52
|
+ *
|
|
|
53
|
+ * @param key Redis键
|
|
|
54
|
+ * @param timeout 超时时间
|
|
|
55
|
+ * @return true=设置成功;false=设置失败
|
|
|
56
|
+ */
|
|
|
57
|
+ public boolean expire(final String key, final long timeout)
|
|
|
58
|
+ {
|
|
|
59
|
+ return expire(key, timeout, TimeUnit.SECONDS);
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ /**
|
|
|
63
|
+ * 设置有效时间
|
|
|
64
|
+ *
|
|
|
65
|
+ * @param key Redis键
|
|
|
66
|
+ * @param timeout 超时时间
|
|
|
67
|
+ * @param unit 时间单位
|
|
|
68
|
+ * @return true=设置成功;false=设置失败
|
|
|
69
|
+ */
|
|
|
70
|
+ public boolean expire(final String key, final long timeout, final TimeUnit unit)
|
|
|
71
|
+ {
|
|
|
72
|
+ return redisTemplate.expire(key, timeout, unit);
|
|
59
|
73
|
}
|
|
60
|
74
|
|
|
61
|
75
|
/**
|
|
|
@@ -64,7 +78,7 @@ public class RedisService
|
|
64
|
78
|
* @param key 缓存键值
|
|
65
|
79
|
* @return 缓存键值对应的数据
|
|
66
|
80
|
*/
|
|
67
|
|
- public <T> T getCacheObject(String key)
|
|
|
81
|
+ public <T> T getCacheObject(final String key)
|
|
68
|
82
|
{
|
|
69
|
83
|
ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
|
70
|
84
|
return operation.get(key);
|
|
|
@@ -75,40 +89,33 @@ public class RedisService
|
|
75
|
89
|
*
|
|
76
|
90
|
* @param key
|
|
77
|
91
|
*/
|
|
78
|
|
- public void deleteObject(String key)
|
|
|
92
|
+ public boolean deleteObject(final String key)
|
|
79
|
93
|
{
|
|
80
|
|
- redisTemplate.delete(key);
|
|
|
94
|
+ return redisTemplate.delete(key);
|
|
81
|
95
|
}
|
|
82
|
96
|
|
|
83
|
97
|
/**
|
|
84
|
98
|
* 删除集合对象
|
|
85
|
99
|
*
|
|
86
|
|
- * @param collection
|
|
|
100
|
+ * @param collection 多个对象
|
|
|
101
|
+ * @return
|
|
87
|
102
|
*/
|
|
88
|
|
- public void deleteObject(Collection collection)
|
|
|
103
|
+ public long deleteObject(final Collection collection)
|
|
89
|
104
|
{
|
|
90
|
|
- redisTemplate.delete(collection);
|
|
|
105
|
+ return redisTemplate.delete(collection);
|
|
91
|
106
|
}
|
|
92
|
107
|
|
|
93
|
108
|
/**
|
|
94
|
109
|
* 缓存List数据
|
|
95
|
110
|
*
|
|
96
|
111
|
* @param key 缓存的键值
|
|
97
|
|
- * @param dataList 待缓存的List数据
|
|
|
112
|
+ * @param values 待缓存的List数据
|
|
98
|
113
|
* @return 缓存的对象
|
|
99
|
114
|
*/
|
|
100
|
|
- public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList)
|
|
|
115
|
+ public <T> long setCacheList(final String key, final List<T> dataList)
|
|
101
|
116
|
{
|
|
102
|
|
- ListOperations listOperation = redisTemplate.opsForList();
|
|
103
|
|
- if (null != dataList)
|
|
104
|
|
- {
|
|
105
|
|
- int size = dataList.size();
|
|
106
|
|
- for (int i = 0; i < size; i++)
|
|
107
|
|
- {
|
|
108
|
|
- listOperation.leftPush(key, dataList.get(i));
|
|
109
|
|
- }
|
|
110
|
|
- }
|
|
111
|
|
- return listOperation;
|
|
|
117
|
+ Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
|
|
|
118
|
+ return count == null ? 0 : count;
|
|
112
|
119
|
}
|
|
113
|
120
|
|
|
114
|
121
|
/**
|
|
|
@@ -117,17 +124,9 @@ public class RedisService
|
|
117
|
124
|
* @param key 缓存的键值
|
|
118
|
125
|
* @return 缓存键值对应的数据
|
|
119
|
126
|
*/
|
|
120
|
|
- public <T> List<T> getCacheList(String key)
|
|
|
127
|
+ public <T> List<T> getCacheList(final String key)
|
|
121
|
128
|
{
|
|
122
|
|
- List<T> dataList = new ArrayList<T>();
|
|
123
|
|
- ListOperations<String, T> listOperation = redisTemplate.opsForList();
|
|
124
|
|
- Long size = listOperation.size(key);
|
|
125
|
|
-
|
|
126
|
|
- for (int i = 0; i < size; i++)
|
|
127
|
|
- {
|
|
128
|
|
- dataList.add(listOperation.index(key, i));
|
|
129
|
|
- }
|
|
130
|
|
- return dataList;
|
|
|
129
|
+ return redisTemplate.opsForList().range(key, 0, -1);
|
|
131
|
130
|
}
|
|
132
|
131
|
|
|
133
|
132
|
/**
|
|
|
@@ -137,15 +136,10 @@ public class RedisService
|
|
137
|
136
|
* @param dataSet 缓存的数据
|
|
138
|
137
|
* @return 缓存数据的对象
|
|
139
|
138
|
*/
|
|
140
|
|
- public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet)
|
|
|
139
|
+ public <T> long setCacheSet(final String key, final Set<T> dataSet)
|
|
141
|
140
|
{
|
|
142
|
|
- BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
|
|
143
|
|
- Iterator<T> it = dataSet.iterator();
|
|
144
|
|
- while (it.hasNext())
|
|
145
|
|
- {
|
|
146
|
|
- setOperation.add(it.next());
|
|
147
|
|
- }
|
|
148
|
|
- return setOperation;
|
|
|
141
|
+ Long count = redisTemplate.opsForSet().add(key, dataSet);
|
|
|
142
|
+ return count == null ? 0 : count;
|
|
149
|
143
|
}
|
|
150
|
144
|
|
|
151
|
145
|
/**
|
|
|
@@ -154,12 +148,9 @@ public class RedisService
|
|
154
|
148
|
* @param key
|
|
155
|
149
|
* @return
|
|
156
|
150
|
*/
|
|
157
|
|
- public <T> Set<T> getCacheSet(String key)
|
|
|
151
|
+ public <T> Set<T> getCacheSet(final String key)
|
|
158
|
152
|
{
|
|
159
|
|
- Set<T> dataSet = new HashSet<T>();
|
|
160
|
|
- BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
|
|
161
|
|
- dataSet = operation.members();
|
|
162
|
|
- return dataSet;
|
|
|
153
|
+ return redisTemplate.opsForSet().members(key);
|
|
163
|
154
|
}
|
|
164
|
155
|
|
|
165
|
156
|
/**
|
|
|
@@ -167,19 +158,12 @@ public class RedisService
|
|
167
|
158
|
*
|
|
168
|
159
|
* @param key
|
|
169
|
160
|
* @param dataMap
|
|
170
|
|
- * @return
|
|
171
|
161
|
*/
|
|
172
|
|
- public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap)
|
|
|
162
|
+ public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
|
|
173
|
163
|
{
|
|
174
|
|
- HashOperations hashOperations = redisTemplate.opsForHash();
|
|
175
|
|
- if (null != dataMap)
|
|
176
|
|
- {
|
|
177
|
|
- for (Map.Entry<String, T> entry : dataMap.entrySet())
|
|
178
|
|
- {
|
|
179
|
|
- hashOperations.put(key, entry.getKey(), entry.getValue());
|
|
180
|
|
- }
|
|
|
164
|
+ if (dataMap != null) {
|
|
|
165
|
+ redisTemplate.opsForHash().putAll(key, dataMap);
|
|
181
|
166
|
}
|
|
182
|
|
- return hashOperations;
|
|
183
|
167
|
}
|
|
184
|
168
|
|
|
185
|
169
|
/**
|
|
|
@@ -188,10 +172,46 @@ public class RedisService
|
|
188
|
172
|
* @param key
|
|
189
|
173
|
* @return
|
|
190
|
174
|
*/
|
|
191
|
|
- public <T> Map<String, T> getCacheMap(String key)
|
|
|
175
|
+ public <T> Map<String, T> getCacheMap(final String key)
|
|
|
176
|
+ {
|
|
|
177
|
+ return redisTemplate.opsForHash().entries(key);
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+ /**
|
|
|
181
|
+ * 往Hash中存入数据
|
|
|
182
|
+ *
|
|
|
183
|
+ * @param key Redis键
|
|
|
184
|
+ * @param hKey Hash键
|
|
|
185
|
+ * @param value 值
|
|
|
186
|
+ */
|
|
|
187
|
+ public <T> void setCacheMapValue(final String key, final String hKey, final T value)
|
|
|
188
|
+ {
|
|
|
189
|
+ redisTemplate.opsForHash().put(key, hKey, value);
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ /**
|
|
|
193
|
+ * 获取Hash中的数据
|
|
|
194
|
+ *
|
|
|
195
|
+ * @param key Redis键
|
|
|
196
|
+ * @param hKey Hash键
|
|
|
197
|
+ * @return Hash中的对象
|
|
|
198
|
+ */
|
|
|
199
|
+ public <T> T getCacheMapValue(final String key, final String hKey)
|
|
|
200
|
+ {
|
|
|
201
|
+ HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
|
|
|
202
|
+ return opsForHash.get(key, hKey);
|
|
|
203
|
+ }
|
|
|
204
|
+
|
|
|
205
|
+ /**
|
|
|
206
|
+ * 获取多个Hash中的数据
|
|
|
207
|
+ *
|
|
|
208
|
+ * @param key Redis键
|
|
|
209
|
+ * @param hKeys Hash键集合
|
|
|
210
|
+ * @return Hash对象集合
|
|
|
211
|
+ */
|
|
|
212
|
+ public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
|
|
192
|
213
|
{
|
|
193
|
|
- Map<String, T> map = redisTemplate.opsForHash().entries(key);
|
|
194
|
|
- return map;
|
|
|
214
|
+ return redisTemplate.opsForHash().multiGet(key, hKeys);
|
|
195
|
215
|
}
|
|
196
|
216
|
|
|
197
|
217
|
/**
|
|
|
@@ -200,7 +220,7 @@ public class RedisService
|
|
200
|
220
|
* @param pattern 字符串前缀
|
|
201
|
221
|
* @return 对象列表
|
|
202
|
222
|
*/
|
|
203
|
|
- public Collection<String> keys(String pattern)
|
|
|
223
|
+ public Collection<String> keys(final String pattern)
|
|
204
|
224
|
{
|
|
205
|
225
|
return redisTemplate.keys(pattern);
|
|
206
|
226
|
}
|