简介
在实际业务开发过程中,有许多需要使用本地缓存的场景,如热点数据缓存、不变数据缓存等。
在这里实现一个通用化的本地缓存框架(类型),起到数据缓存、缓存定时清理等作用。
设计
失效缓存清理
程序流程图:
说明:
- 缓存类内部实现缓存数据模型,包含缓存数据、缓存时刻。
1 | private class CacheModel { |
- 遍历缓存数据对象,使用当前时间与该对象缓存时刻进行对比,若超过设置的缓存失效时间,则清理该对象。
数据按需加载
程序流程图:
说明:
- 从缓存获取数据,首先判断缓存数据是否存在,若存在,直接返回缓存数据。
- 缓存数据不存在,则调用缓存对象初始化时候配置的数据获取对象,获取对应数据。
1 | public interface CacheAcquireOperation<KEY, VALUE> { |
核心代码片段
缓存清理
1 | private void initCacheCleanTask(String name) { |
缓存获取
1 | public VALUE get(KEY key) { |
用法示例
1 | public static void main(String[] args) throws Exception { |
输出结果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
52Cache acquire operation, key: 0-name, value: 0-name-1569833192414
0-name-1569833192414
Cache acquire operation, key: 1-name, value: 1-name-1569833192436
1-name-1569833192436
Cache acquire operation, key: 2-name, value: 2-name-1569833192437
2-name-1569833192437
Cache acquire operation, key: 3-name, value: 3-name-1569833192437
3-name-1569833192437
Cache acquire operation, key: 4-name, value: 4-name-1569833192438
4-name-1569833192438
Cache acquire operation, key: 5-name, value: 5-name-1569833192438
5-name-1569833192438
Cache acquire operation, key: 6-name, value: 6-name-1569833192438
6-name-1569833192438
Cache acquire operation, key: 7-name, value: 7-name-1569833192439
7-name-1569833192439
Cache acquire operation, key: 8-name, value: 8-name-1569833192439
8-name-1569833192439
Cache acquire operation, key: 9-name, value: 9-name-1569833192439
9-name-1569833192439
----------------------
0-name-1569833192414
1-name-1569833192436
2-name-1569833192437
3-name-1569833192437
4-name-1569833192438
5-name-1569833192438
6-name-1569833192438
7-name-1569833192439
8-name-1569833192439
9-name-1569833192439
----------------------
Cache acquire operation, key: 0-name, value: 0-name-1569833217448
0-name-1569833217448
Cache acquire operation, key: 1-name, value: 1-name-1569833217449
1-name-1569833217449
Cache acquire operation, key: 2-name, value: 2-name-1569833217449
2-name-1569833217449
Cache acquire operation, key: 3-name, value: 3-name-1569833217450
3-name-1569833217450
Cache acquire operation, key: 4-name, value: 4-name-1569833217450
4-name-1569833217450
Cache acquire operation, key: 5-name, value: 5-name-1569833217450
5-name-1569833217450
Cache acquire operation, key: 6-name, value: 6-name-1569833217450
6-name-1569833217450
Cache acquire operation, key: 7-name, value: 7-name-1569833217450
7-name-1569833217450
Cache acquire operation, key: 8-name, value: 8-name-1569833217450
8-name-1569833217450
Cache acquire operation, key: 9-name, value: 9-name-1569833217451
9-name-1569833217451