Repository navigation
Expand file tree
/
Copy pathCacheCore.java
More file actions
63 lines (52 loc) · 1.42 KB
/
Copy pathCacheCore.java
File metadata and controls
63 lines (52 loc) · 1.42 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
package bms.core.common;
import java.util.Date;
import java.util.Hashtable;
import bms.core.model.CacheItem;
/**
* @author xu.jian
*
*/
public class CacheCore {
private static Hashtable<String, Object> cacheList;
static {
cacheList = new Hashtable<String, Object>();
}
public synchronized static void set(String key, Object value) {
if (key != null && value != null) {
cacheList.put(key, value);
}
}
public synchronized static void setItem(String key, Object value) {
if (key != null && value != null) {
CacheItem ci = new CacheItem(key, value, new Date().getTime());
cacheList.put(key, ci);
}
}
public static Object get(String key) {
return cacheList.get(key);
}
public static Object getItem(String key) {
if (isExpired(key))
return null;
CacheItem ci = (CacheItem) cacheList.get(key);
return ci.getValue();
}
public synchronized static Object remove(String key) {
return cacheList.remove(key);
}
public static boolean isExpired(String key) {
if (!contains(key))
return true;
CacheItem item = (CacheItem) cacheList.get(key);
if (item == null)
return true;
long cache_Interval = new Date().getTime()
- (item.getExpiredTime() + 5 * 60 * 1000); // 统一5分钟过期
if (cache_Interval > 0)
return true;
return false;
}
public static boolean contains(String key) {
return cacheList.containsKey(key);
}
}