xiangpei
2025-05-30 32affb9328997ab51b2c88f7f034c16a1e7ed6d0
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
package cn.lili.cache;
 
 
import org.springframework.data.redis.core.ZSetOperations;
 
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
/**
 * 缓存接口
 *
 * @author Chopper
 */
public interface Cache<T> {
 
    /**
     * Get an item from the cache, nontransactionally
     *
     * @param key 缓存key
     * @return the cached object or <tt>null</tt>
     */
    T get(Object key);
 
    /**
     * Get an item from the cache, nontransactionally
     *
     * @param key 缓存key
     * @return the cached object or <tt>null</tt>
     */
    String getString(Object key);
 
 
    /**
     * multiGet
     *
     * @param keys 要查询的key集合
     * @return 集合
     */
    List multiGet(Collection keys);
 
    /**
     * 批量set
     *
     * @param map 键值对
     */
    void multiSet(Map map);
 
 
    /**
     * 批量删除
     *
     * @param keys 要删除的key集合
     */
    void multiDel(Collection keys);
 
    /**
     * Add an item to the cache, nontransactionally, with
     * failfast semantics
     *
     * @param key   缓存key
     * @param value 缓存value
     */
    void put(Object key, T value);
 
    /**
     * 往缓存中写入内容
     *
     * @param key   缓存key
     * @param value 缓存value
     * @param exp   超时时间,单位为秒
     */
    void put(Object key, T value, Long exp);
 
    /**
     * 往缓存中写入内容
     *
     * @param key      缓存key
     * @param value    缓存value
     * @param exp      过期时间
     * @param timeUnit 过期单位
     */
    void put(Object key, T value, Long exp, TimeUnit timeUnit);
 
    /**
     * 删除
     *
     * @param key 缓存key
     */
    Boolean remove(Object key);
 
    /**
     * 删除
     *
     * @param key 缓存key
     */
    void vagueDel(Object key);
 
    /**
     * Clear the cache
     */
    void clear();
 
 
    /**
     * 往缓存中写入内容
     *
     * @param key       缓存key
     * @param hashKey   缓存中hashKey
     * @param hashValue hash值
     */
    void putHash(Object key, Object hashKey, Object hashValue);
 
    /**
     * 玩缓存中写入内容
     *
     * @param key 缓存key
     * @param map map value
     */
    void putAllHash(Object key, Map map);
 
    /**
     * 读取缓存值
     *
     * @param key     缓存key
     * @param hashKey map value
     * @return 返回缓存中的数据
     */
    T getHash(Object key, Object hashKey);
 
    /**
     * 读取缓存值
     *
     * @param key 缓存key
     * @return 缓存中的数据
     */
    Map<Object, Object> getHash(Object key);
 
    /**
     * 是否包含
     *
     * @param key 缓存key
     * @return 缓存中的数据
     */
    boolean hasKey(Object key);
 
 
    /**
     * 模糊匹配key
     *
     * @param pattern 模糊key
     * @return 缓存中的数据
     */
    List<Object> keys(String pattern);
 
    /**
     * 原生阻塞keys 不推荐使用
     *
     * @param pattern 模糊key
     * @return 缓存中的数据
     */
    List<Object> keysBlock(String pattern);
 
 
    //-----------------------------------------------用于特殊场景,redis去重计数---------------------------------------------
 
    /**
     * 累计数目
     * 效率较高的 计数器
     * 如需清零,按照普通key 移除即可
     *
     * @param key   key值
     * @param value 去重统计值
     * @return 计数器结果
     */
    Long cumulative(Object key, Object value);
 
    /**
     * 计数器结果
     * <p>
     * 效率较高的 计数器 统计返回
     * 如需清零,按照普通key 移除即可
     *
     * @param key 计数器key
     * @return 计数器结果
     */
    Long counter(Object key);
 
    /**
     * 批量计数
     *
     * @param keys 要查询的key集合
     * @return 批量计数
     */
    List multiCounter(Collection keys);
 
    /**
     * 计数器结果
     * <p>
     * 效率较高的 计数器 统计返回
     * 如需清零,按照普通key 移除即可
     *
     * @param key key值
     * @return 计数器结果
     */
    Long mergeCounter(Object... key);
    //---------------------------------------------------用于特殊场景,redis去重统计-----------------------------------------
 
    //-----------------------------------------------redis计数---------------------------------------------
 
    /**
     * redis 计数器 累加
     * 注:到达liveTime之后,该次增加取消,即自动-1,而不是redis值为空
     *
     * @param key      为累计的key,同一key每次调用则值 +1
     * @param liveTime 单位秒后失效
     * @return 计数器结果
     */
    Long incr(String key, long liveTime);
 
    /**
     * redis 计数器 累加
     * 注:到达liveTime之后,该次增加取消,即自动-1,而不是redis值为空
     *
     * @param key 为累计的key,同一key每次调用则值 +1
     * @return 计数器结果
     */
    Long incr(String key);
 
    /**
     * redis 自减一
     *
     * @param key
     * @return
     */
    Long decr(String key);
 
    //-----------------------------------------------redis计数---------------------------------------------
 
    /**
     * 使用Sorted Set记录keyword
     * zincrby命令,对于一个Sorted Set,存在的就把分数加x(x可自行设定),不存在就创建一个分数为1的成员
     *
     * @param sortedSetName sortedSetName的Sorted Set不用预先创建,不存在会自动创建,存在则向里添加数据
     * @param keyword       关键词
     */
    void incrementScore(String sortedSetName, String keyword);
 
    /**
     * 使用Sorted Set记录keyword
     * zincrby命令,对于一个Sorted Set,存在的就把分数加x(x可自行设定),不存在就创建一个分数为1的成员
     *
     * @param sortedSetName sortedSetName的Sorted Set不用预先创建,不存在会自动创建,存在则向里添加数据
     * @param keyword       关键词
     * @param score         分数
     */
    void incrementScore(String sortedSetName, String keyword, Integer score);
 
    /**
     * zrevrange命令, 查询Sorted Set中指定范围的值
     * 返回的有序集合中,score大的在前面
     * zrevrange方法无需担心用于指定范围的start和end出现越界报错问题
     *
     * @param sortedSetName sortedSetName
     * @param start         查询范围开始位置
     * @param end           查询范围结束位置
     * @return 获取满足条件的集合
     */
    Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String sortedSetName, Integer start, Integer end);
 
    /**
     * zrevrange命令, 查询Sorted Set中指定范围的值
     * 返回的有序集合中,score大的在前面
     * zrevrange方法无需担心用于指定范围的start和end出现越界报错问题
     *
     * @param sortedSetName sortedSetName
     * @param count         查询数量
     * @return 获取满足条件的集合
     */
    Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String sortedSetName, Integer count);
 
 
    /**
     * 向Zset里添加成员
     *
     * @param key   key值
     * @param score 分数
     * @param value 值
     * @return 增加状态
     */
    boolean zAdd(String key, long score, String value);
 
 
    /**
     * 获取 某key 下 某一分值区间的队列
     *
     * @param key  缓存key
     * @param from 开始时间
     * @param to   结束时间
     * @return 数据
     */
    Set<ZSetOperations.TypedTuple<Object>> zRangeByScore(String key, int from, long to);
 
    /**
     * 移除 Zset队列值
     *
     * @param key   key值
     * @param value 删除的集合
     * @return 删除数量
     */
    Long zRemove(String key, String... value);
 
    /**
     * 设置key过期时间
     *
     * @param key
     * @param timeout 过期时长
     * @param timeUnit 时长单位
     */
    void setExpire(String key, long timeout, TimeUnit timeUnit);
 
    /**
     * 检查key是否存在
     *
     * @param key
     * @return
     */
    boolean exist(String key);
}