-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheCore.java
More file actions
142 lines (120 loc) · 3.22 KB
/
CacheCore.java
File metadata and controls
142 lines (120 loc) · 3.22 KB
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
package cache.core;
import cache.Cache;
import cache.ICacheEvict;
import cache.ICacheEvictContext;
import cache.ICacheExpire;
import cache.context.evict.CacheEvictContext;
import java.security.Key;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class CacheCore<K, V> implements Cache<K, V> {
private Map<K, V> cacheMap;//用来缓存用的map
private int sizeLimit;// 限制缓存大小
private ICacheExpire<K, V> iCacheExpire; //过期策略
private ICacheEvict<K, V> iCacheEvict; //数据满时的数据淘汰策略
/**
* ]
*
* @param key 需要过期的key
* @param expireTime key的保质期
* @return
*/
@Override
public Cache<K, V> expire(K key, long expireTime) {
// 当前时间+expireTime = k-v过期的时间点
long theTimeToExpire = System.currentTimeMillis() + expireTime;
this.expireAt(key, theTimeToExpire);
return this;
}
public Cache<K, V> setSizeLimit(int sizeLimit) {
this.sizeLimit = sizeLimit;
return this;
}
@Override
public ICacheEvict<K, V> evict() {
return this.iCacheEvict;
}
@Override
public Cache<K, V> expireStrategy(ICacheExpire<K, V> expire) {
this.iCacheExpire = expire;
return this;
}
@Override
public Cache<K, V> evictStrategy(ICacheEvict<K, V> evict) {
this.iCacheEvict = evict;
return this;
}
@Override
public Cache<K, V> cacheMap(Map<K, V> map) {
this.cacheMap = map;
return this;
}
/**
* @param key
* @param time
* @return
*/
@Override
public Cache<K, V> expireAt(K key, long time) {
this.iCacheExpire.expire(key, time);
return this;
}
@Override
public int size() {
return cacheMap.size();
}
@Override
public boolean isEmpty() {
return cacheMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return cacheMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return cacheMap.containsKey(value);
}
@Override
public V get(Object key) {
return this.cacheMap.get(key);
}
@Override
public V put(K key, V value) {
CacheEvictContext<K, V> cacheEvictContext = new CacheEvictContext<>();
cacheEvictContext.setCache(this).setKey(key).setLimit(this.sizeLimit);
this.iCacheEvict.evict(cacheEvictContext);
return this.cacheMap.put(key, value);
}
/**
* 执行清除操作时直接代用用于储存缓存的map的remove方法
*
* @param key
* @return
*/
@Override
public V remove(Object key) {
return this.cacheMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
this.cacheMap.putAll(m);
}
@Override
public void clear() {
this.cacheMap.clear();
}
@Override
public Set<K> keySet() {
return this.cacheMap.keySet();
}
@Override
public Collection<V> values() {
return this.cacheMap.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return this.cacheMap.entrySet();
}
}