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
package com.yanzuoguang.util.cache;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.Date;
import java.util.Hashtable;
/**
* 内存缓存
*
* @param <T>
* @author 颜佐光
*/
public class MemoryCache<T> {
/**
* 缓存的对象
*/
private Hashtable<String, MemoryCacheItem<T>> cache = new Hashtable<String, MemoryCacheItem<T>>();
/**
* 清除时间
*/
private int clearSecond;
/**
* 是否自动清除缓存
*/
private boolean isAutoClear;
/**
* 读取时是否激活
*/
private boolean isGetActive;
/**
* 构造函数,不会自动清除对象
*/
public MemoryCache() {
this(false, 0);
}
/**
* 构造函数
*
* @param clearSecond 清除缓存的时间
*/
public MemoryCache(int clearSecond) {
this(true, clearSecond);
}
/**
* 构造函数
*
* @param isAutoClear 是否自动清除
* @param clearSecond 清除缓存的时间
*/
public MemoryCache(boolean isAutoClear, int clearSecond) {
this.isAutoClear = isAutoClear;
this.clearSecond = clearSecond;
this.isGetActive = false;
if (isAutoClear) {
MemoryCacheCenter.CLEAR_LIST.add(this);
}
}
public boolean isGetActive() {
return isGetActive;
}
public void setGetActive(boolean getActive) {
isGetActive = getActive;
}
public int getClearSecond() {
return clearSecond;
}
public void setClearSecond(int clearSecond) {
this.clearSecond = clearSecond;
}
/**
* 写入数据
*
* @param key 关键字
* @param data 数据
*/
public synchronized T put(String key, T data) {
if (this.isAutoClear && this.clearSecond < 1) {
return data;
}
MemoryCacheItem<T> item = cache.containsKey(key) ? cache.get(key) : null;
if (item == null) {
item = new MemoryCacheItem<T>();
cache.put(key, item);
}
item.setData(data);
if (StringHelper.isEmpty(item.getName())) {
item.setName(key);
cache.put(key, item);
}
return item.getData();
}
/**
* 读取数据
*
* @param key 关键字
*/
public synchronized T get(String key) {
if (this.isAutoClear && this.clearSecond < 1) {
return null;
}
MemoryCacheItem<T> item = cache.get(key);
if (item == null) {
return null;
}
this.getActive(item);
return item.getData();
}
/**
* 读取数据
*
* @param key 关键字
* @param defData 默认数据
* @return
*/
public synchronized T get(String key, T defData) {
if (this.isAutoClear && this.clearSecond < 1) {
return defData;
}
MemoryCacheItem<T> item = cache.getOrDefault(key, new MemoryCacheItem<T>());
if (StringHelper.isEmpty(item.getName())) {
item.setName(key);
item.setData(defData);
cache.put(key, item);
} else {
if (defData instanceof MemoryCache) {
((MemoryCache) defData).close();
}
}
this.getActive(item);
return item.getData();
}
/**
* 在获取时重新激活数据
*/
private void getActive(MemoryCacheItem<T> item) {
if (isGetActive) {
item.active();
}
}
/**
* 删除一个数据
*
* @param key 关键字
*/
public synchronized T remove(String key) {
MemoryCacheItem<T> item = cache.remove(key);
if (item == null) {
return null;
}
T data = item.getData();
if (data instanceof MemoryCache) {
((MemoryCache) data).close();
}
return data;
}
/**
* 清除所有的缓存
*/
public synchronized void clear() {
Object[] keys = cache.keySet().toArray();
for (Object keyObject : keys) {
// 关键字
String key = keyObject.toString();
this.remove(key);
}
}
/**
* 清除超时的数据
*/
public synchronized void clearTimeout() {
if (this.clearSecond < 0) {
return;
}
Object[] keys = cache.keySet().toArray();
Date now = new Date();
for (Object keyObject : keys) {
// 关键字
String key = keyObject.toString();
// 获取到数据
MemoryCacheItem<T> itemValue = this.cache.get(key);
if (itemValue == null) {
continue;
}
// 判断是否过期
if (now.getTime() - itemValue.getDate().getTime() > this.clearSecond * 1000) {
this.remove(key);
}
}
}
/**
* 将缓存对象从缓存对象中清除
*/
public synchronized void close() {
this.clear();
MemoryCacheCenter.CLEAR_LIST.remove(this);
}
/**
* 获取所有的关键字
*
* @return
*/
public synchronized String[] getKeys() {
String[] items = new String[this.cache.size()];
return this.cache.keySet().toArray(items);
}
/**
* 析构函数
*
* @throws Throwable
*/
@Override
protected void finalize(){
this.close();
}
}