# 功能描述

本缓存组件支持一级缓存和二级缓存,一级缓存使用本地缓存,二级缓存使用redis进行缓存。
同时支持对权限、表结构和sql执行结果的缓存,具体设置见缓存配置一览表。
配置文件为application-cache.yml

提示

(1) 需在application.yml中增加spring.profiles.active中增加cache才能激活配置文件
(2) 需设置application-cache.yml中的spring.redis.enabled为true才能启动redis服务

# 配置一览表

spring:
  redis:
    enabled: false #设置为true才启动redis服务
    host: 127.0.0.1
    port: 6379
    password:
    timeout: 30s  #连接超时,时间为秒
    database: 0
#    cluster:
#      nodes:
#        - 192.168.56.128:7000
#        - 192.168.56.128:7001
#        - 192.168.56.128:7002
    lettuce:
      pool:
        max-active: 100 # 连接池最大连接数 默认8 ,负数表示没有限制
        max-wait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
        max-idle: 10   # 连接池中的最大空闲连接 默认8
        min-idle: 5   # 连接池中的最小空闲连接 默认0

#######  缓存内容 配置 ########
sei:
  cloud:
    cache:
      local-cache:
        enabled: true #设置为true才启动一级缓存
        default-timeout: 1800 #本地缓存缺省时间(), 默认为30分钟
      privilege:
        time-out: 300  #用户权限缓存过期时间(分钟),默认为180分钟
      strc:
        time-out: 600  #表结构缓存过期时间(分钟),默认为60分钟
      sql:
        time-out: 30  #sql语句执行结果缓存过期时间(分钟),默认为30分钟
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

# 一级缓存配置

一级缓存在本地,只有设置配置文件中的sei.cloud.cache.local-cache.enabled为true才开启一级缓存,如下图所示:

sei:
  cloud:
    cache:
      local-cache:
        enabled: true #设置为true才启动一级缓存
1
2
3
4
5

# 二级缓存配置

二级缓存为redis缓存,只有设置配置文件中的spring.redis.enabled为true才开启一级缓存,如下图所示:

spring:
  redis:
    enabled: true #设置为true才启动redis服务
1
2
3

提示

二级缓存支持集群配置,参加配置文件。

# Service接口

# 第一步 引入缓存包

<dependency>
    <groupId>sei-cloud</groupId>
    <artifactId>cache</artifactId>
</dependency>
1
2
3
4

# 第二步 引入接口

@Resource
    CacheService cacheService;
1
2

# 第三步 使用接口

接口方法主要提供对象缓存和字符串缓存,非对象尽量采用字符串缓存以便节省序列化转换从而加快访问速度。

/**
 * 缓存接口
 * @author xiong
 */
public interface CacheService {

    /**
     * 获得redis服务器信息
     * @return Map
     */
    Map<String, Object> getRedisInfo();

    /**
     * 获得本地缓存信息
     * @return List
     */
    List<Map<String, Object>> getLocalCacheInfo();

    /**
     * 获得redis自增长序列号(注意获取后redis中该值会自动加步长值)
     * @param key: 名称key
     * @param initNum: 初始值
     * @param step: 步长
     * @param date: 过期截止日期
     * @return Long
     */
    Long getAtomicLong(@NonNull String key, long initNum, int step, @NonNull Date date);

    /**
     * 获得redis自增长序列号(注意获取后redis中该值会自动加1)
     * @param key: 名称key
     * @return Long
     */
    Long getAtomicLong(@NonNull String key);

    /**
     * 缓存对象,整体缓存
     * @param key: 名称
     * @param obj: 值
     */
    void setObject(@NonNull String key, @Nullable Object obj);

    /**
     * 缓存对象,整体缓存
     * @param key: 名称
     * @param obj: 值
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setObject(@NonNull String key, @Nullable Object obj, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 从缓存中获得键值key的对象
     * @param key: 全局唯一键
     * @param <T> T
     * @return T | null
     */
    <T> T getObject(@NonNull String key);

    /**
     * 从缓存中获得键值key的对象
     * @param key: 全局唯一键
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @param <T> T
     * @return T | null
     */
    <T> T getObject(@NonNull String key, boolean onlyRedis);

    /**
     * 从缓存中获得键值key的对象
     * @param key: 名称
     * @param <T> T
     * @return T | null
     */
    <T> T getObjectAndDel(@NonNull String key);

    /**
     * 缓存字符串
     * @param key: 名称
     * @param str: 值
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setString(@NonNull String key, @Nullable String str, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 缓存字符串
     * @param key: 名称
     * @param str: 值
     */
    void setString(@NonNull String key, @Nullable String str);

    /**
     * 从缓存中获得键值key的字符串
     * @param key: 名称
     * @return String | null
     */
    String getString(@NonNull String key);

    /**
     * 从缓存中获得键值key的字符串
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return String | null
     */
    String getString(@NonNull String key, boolean onlyRedis);

    /**
     * 从缓存中获得键值key的字符串并删除该key
     * @param key: 名称
     * @return String | null
     */
    String getStringAndDel(@NonNull String key);

    /**
     * 缓存键值为key的map字符串对象,注意map内容存放时会自动分开存放
     * @param key: 名称
     * @param map: Map对象
     */
    void setStringMap(@NonNull String key, @Nullable Map<String, String> map);

    /**
     * 缓存键值为key的map字符串对象,注意map内容存放时会自动分开存放
     * @param key: 名称
     * @param map: Map对象
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setStringMap(@NonNull String key, @Nullable Map<String, String> map, long expireTime, @Nullable TimeUnit timeUnit);

    /**
     * 缓存键值为key的map字符串对象,注意map内容存放时会自动分开存放
     * @param mapKey: 名称
     * @param itemKey: 子项名称
     * @param str: 值
     */
    void setStringMapItem(@NonNull String mapKey, @NonNull String itemKey, @Nullable String str);

    /**
     * 缓存键值为key的map字符串对象,注意map内容存放时会自动分开存放
     * @param mapKey: 名称
     * @param itemKey: 子项名称
     * @param str: 值
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setStringMapItem(@NonNull String mapKey, @NonNull String itemKey, @Nullable String str, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 缓存键值为key的map对象,注意map内容存放时会自动分开存放
     * @param key: 名称
     * @param map: Map对象
     */
    void setObjectMap(@NonNull String key, @Nullable Map<String, ?> map);

    /**
     * 缓存键值为key的map对象,注意map内容存放时会自动分开存放
     * @param key: 名称
     * @param map: Map对象
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setObjectMap(@NonNull String key, @Nullable Map<String, Object> map, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 缓存键值为key的map对象,注意map内容存放时会自动分开存放
     * @param mapKey: 名称
     * @param itemKey: 子项名称
     * @param object: 值
     */
    void setObjectMapItem(@NonNull String mapKey, @NonNull String itemKey, @Nullable Object object);

    /**
     * 缓存键值为key的map对象,注意map内容存放时会自动分开存放
     * @param mapKey: 名称
     * @param itemKey: 子项名称
     * @param object: 值
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setObjectMapItem(@NonNull String mapKey, @NonNull String itemKey, @Nullable Object object, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 获得键值key的字段的字符串map对象
     * @param key: 名称
     * @return Map | null
     */
    Map getObjectMap(@NonNull String key);

    /**
     * 获得键值key的字段的字符串map对象
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return Map | null
     */
    Map getObjectMap(@NonNull String key, boolean onlyRedis);

    /**
     * 获取Map对象中的一个子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @param <T> T
     * @return T | null
     */
    <T> T getObjectMapItem(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 获取Map对象中的一个子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @param <T> T
     * @return T | null
     */
    <T> T getObjectMapItem(@NonNull String mapKey, @NonNull String mapItemKey, boolean onlyRedis);

    /**
     * 获取Map对象中的一个子项并删除该子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @param <T> T
     * @return T | null
     */
    <T> T getObjectMapItemAndDel(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 获取map的子项个数
     * @param key: 名称
     * @return long
     */
    long getMapItemCount(@NonNull String key);

    /**
     * 获取map的子项个数
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return long
     */
    long getMapItemCount(@NonNull String key, boolean onlyRedis);

    /**
     * 获取map的子项的名称集合
     * @param mapKey: 名称
     * @return Set
     */
    Set<Object> getMapItemNames(@NonNull String mapKey);

    /**
     * 获取map的子项的名称集合
     * @param mapKey: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return Set
     */
    Set<Object> getMapItemNames(@NonNull String mapKey, boolean onlyRedis);

    /**
     * 返回字符串map
     * @param key: 名称
     * @return Map | null
     */
    Map getStringMap(@NonNull String key);

    /**
     * 返回字符串map
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return Map | null
     */
    Map getStringMap(@NonNull String key, boolean onlyRedis);

    /**
     * 获取字符串Map的一个子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @return String | null
     */
    String getStringMapItem(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 获取字符串Map的一个子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return String | null
     */
    String getStringMapItem(@NonNull String mapKey, @NonNull String mapItemKey, boolean onlyRedis);

    /**
     * 获取字符串Map的一个子项并删除
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @return String | null
     */
    String getStringMapItemAndDel(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 获取map的子项个数
     * @param key: 名称
     * @return long
     */
    long getStringMapItemCount(@NonNull String key);

    /**
     * 获取map的子项个数
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return long
     */
    long getStringMapItemCount(@NonNull String key, boolean onlyRedis);

    /**
     * 获取map的子项的名称集合
     * @param mapKey: 名称
     * @return Set
     */
    Set<Object> getStringMapItemNames(@NonNull String mapKey);

    /**
     * 删除Map中的一个子项
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     */
    void delMapItem(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 删除指定的key,包含本地缓存的和redis服务器的
     * @param key: 名称
     */
    void delKey(@NonNull String key);

    /**
     * 删除指定的key集合,包含本地缓存的和redis服务器的
     * @param keys: 名称集合
     */
    void delKey(@Nullable Collection keys);

    /**
     * 删除指定的Map中的一个子项,包含本地缓存的和redis服务器的
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     */
    void delKey(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 删除正则表达式匹配的值
     * @param regex: 正则表达式
     */
    void delMatchKey(@NonNull String regex);

    /**
     * 延长key的过期时间
     * @param key: 名称
     * @param expireTime: 过期时间
     * @param timeUnit: 过期时间单位
     */
    void setExpireTime(@NonNull String key, long expireTime, @NonNull TimeUnit timeUnit);

    /**
     * 查看key是否存在
     * @param key: 名称
     * @return boolean
     */
    boolean keyExists(@NonNull String key);

    /**
     * 查看key是否存在
     * @param key: 名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return boolean
     */
    boolean keyExists(@NonNull String key, boolean onlyRedis);

    /**
     * 查看map中的一个子项是否存在
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @return boolean
     */
    boolean keyExists(@NonNull String mapKey, String mapItemKey);

    /**
     * 查看map中的一个子项是否存在
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     * @param onlyRedis: 是否忽略本地缓存而只拿redis
     * @return boolean
     */
    boolean keyExists(@NonNull String mapKey, @NonNull String mapItemKey, boolean onlyRedis);

    /**
     * 获得redis所有Key的名称
     * @param regex: 正则表达式
     * @return Set
     */
    Set<String> getKeys(@NonNull String regex);

    /**
     * 获得所有key的个数
     * @param regex: 正则表达式
     * @return int
     */
    long getKeysCount(@NonNull String regex);

    /**
     * 获得redis字符操作
     * @return StringRedisTemplate
     */
    StringRedisTemplate getStringRedisTemplate();

    /**
     * 获得redis对象操作
     * @return RedisTemplate
     */
    RedisTemplate getRedisTemplate();

    /**
     * 获得本地缓存对象
     * @return Cache | null
     */
    Map getLocalCache();

    /**
     * 只缓存到到本地,不缓存到redis
     * @param key: 名称
     * @param value: 值
     */
    void setLocalObject(@NonNull String key, @Nullable Object value);

    /**
     * 只从本地缓存中拿值
     * @param key: 名称
     * @return T | null
     */
    <T> T getLocalObject(@NonNull Object key);

    /**
     * 删除本地缓存值
     * @param key: 名称
     */
    void delLocalKey(@NonNull String key);

    /**
     * 删除本地缓存值
     * @param mapKey: 名称
     * @param mapItemKey: 子项名称
     */
    void delLocalKey(@NonNull String mapKey, @NonNull String mapItemKey);

    /**
     * 清除所有缓存
     */
    void clearCache();

}
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

# 消息订阅

缓存如果开启了redis缓存,则可使用redis缓存进行通知服务,消息处理类接口为:

点击查看RedisMessageService消息处理类接口
/**
 * redis频道消息接口
 * @author xiong
 */
public interface RedisMessageService {

   /**
    * 向频道发送对象消息
    * @param channel: 通道
    * @param msg: 消息内容
    */
   void sendObjectMsg(@NonNull String channel, @NonNull Object msg);

   /**
    * 向频道发送字符串消息
    * @param channel: 通道
    * @param msg: 消息内容
    */
   void sendStringMsg(@NonNull String channel, @NonNull String msg);

   /**
    * 注册redis的订阅频道
    * @param name: 要订阅的频道名称
    * @param messageListener: 消息处理类,该类必须继承jedisPubSub
    */
   void registerRedisChannel(@NonNull String name, @NonNull MessageListener messageListener);

   /**
    * 取消订阅
    * @param messageListener: MessageListener
    * @param name: 频道名称
    */
   void removeRedisChannel(@NonNull MessageListener messageListener, @NonNull String name);

}
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

# 第一步 引入接口

@Resource
    RedisMessageService redisMessageService;
1
2

# 第二步 创建消息处理类

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
public class CacheMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] pattern) {
        //...  接收到消息后对消息的处理程序
    }
}
1
2
3
4
5
6
7
8

# 第三步 注册频道

注册频道名称并将传入第二步的处理类,例如频道名称为demo:

redisMessageService.registerRedisChannel("demo", new CacheMessageListener());
1

或者将第二步与第三步合并写法:

redisMessageService.registerRedisChannel("demo", new MessageListener() {
    @Override
    public void onMessage(Message message, byte[] bytes) {
        //...  接收到消息后对消息的处理程序
    }
});
1
2
3
4
5
6

# 第四步 发送消息

系统提供2种发送消息的接口,一种是发送字符串,另一种是发送对象,接口分别为:
sendStringMsg / sendObjectMsg
例如发送字符串"123"到频道"demo"则程序为:

redisMessageService.sendStringMsg("demo", "123");
1

# 第五步 取消订阅

如果要取消订阅,则调用接口的removeRedisChannel方法即可取消订阅