提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.core.util; |
H |
2 |
|
|
3 |
import java.util.concurrent.ConcurrentHashMap; |
|
4 |
import java.util.concurrent.ConcurrentMap; |
|
5 |
|
|
6 |
/** |
|
7 |
* local cache tool |
|
8 |
* |
|
9 |
* @author xuxueli 2018-01-22 21:37:34 |
|
10 |
*/ |
|
11 |
public class LocalCacheUtil { |
|
12 |
|
|
13 |
private static ConcurrentMap<String, LocalCacheData> cacheRepository = new ConcurrentHashMap<String, LocalCacheData>(); // 类型建议用抽象父类,兼容性更好; |
|
14 |
private static class LocalCacheData{ |
|
15 |
private String key; |
|
16 |
private Object val; |
|
17 |
private long timeoutTime; |
|
18 |
|
|
19 |
public LocalCacheData() { |
|
20 |
} |
|
21 |
|
|
22 |
public LocalCacheData(String key, Object val, long timeoutTime) { |
|
23 |
this.key = key; |
|
24 |
this.val = val; |
|
25 |
this.timeoutTime = timeoutTime; |
|
26 |
} |
|
27 |
|
|
28 |
public String getKey() { |
|
29 |
return key; |
|
30 |
} |
|
31 |
|
|
32 |
public void setKey(String key) { |
|
33 |
this.key = key; |
|
34 |
} |
|
35 |
|
|
36 |
public Object getVal() { |
|
37 |
return val; |
|
38 |
} |
|
39 |
|
|
40 |
public void setVal(Object val) { |
|
41 |
this.val = val; |
|
42 |
} |
|
43 |
|
|
44 |
public long getTimeoutTime() { |
|
45 |
return timeoutTime; |
|
46 |
} |
|
47 |
|
|
48 |
public void setTimeoutTime(long timeoutTime) { |
|
49 |
this.timeoutTime = timeoutTime; |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
/** |
|
55 |
* set cache |
|
56 |
* |
|
57 |
* @param key |
|
58 |
* @param val |
|
59 |
* @param cacheTime |
|
60 |
* @return |
|
61 |
*/ |
|
62 |
public static boolean set(String key, Object val, long cacheTime){ |
|
63 |
|
|
64 |
// clean timeout cache, before set new cache (avoid cache too much) |
|
65 |
cleanTimeoutCache(); |
|
66 |
|
|
67 |
// set new cache |
|
68 |
if (key==null || key.trim().length()==0) { |
|
69 |
return false; |
|
70 |
} |
|
71 |
if (val == null) { |
|
72 |
remove(key); |
|
73 |
} |
|
74 |
if (cacheTime <= 0) { |
|
75 |
remove(key); |
|
76 |
} |
|
77 |
long timeoutTime = System.currentTimeMillis() + cacheTime; |
|
78 |
LocalCacheData localCacheData = new LocalCacheData(key, val, timeoutTime); |
|
79 |
cacheRepository.put(localCacheData.getKey(), localCacheData); |
|
80 |
return true; |
|
81 |
} |
|
82 |
|
|
83 |
/** |
|
84 |
* remove cache |
|
85 |
* |
|
86 |
* @param key |
|
87 |
* @return |
|
88 |
*/ |
|
89 |
public static boolean remove(String key){ |
|
90 |
if (key==null || key.trim().length()==0) { |
|
91 |
return false; |
|
92 |
} |
|
93 |
cacheRepository.remove(key); |
|
94 |
return true; |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* get cache |
|
99 |
* |
|
100 |
* @param key |
|
101 |
* @return |
|
102 |
*/ |
|
103 |
public static Object get(String key){ |
|
104 |
if (key==null || key.trim().length()==0) { |
|
105 |
return null; |
|
106 |
} |
|
107 |
LocalCacheData localCacheData = cacheRepository.get(key); |
|
108 |
if (localCacheData!=null && System.currentTimeMillis()<localCacheData.getTimeoutTime()) { |
|
109 |
return localCacheData.getVal(); |
|
110 |
} else { |
|
111 |
remove(key); |
|
112 |
return null; |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* clean timeout cache |
|
118 |
* |
|
119 |
* @return |
|
120 |
*/ |
|
121 |
public static boolean cleanTimeoutCache(){ |
|
122 |
if (!cacheRepository.keySet().isEmpty()) { |
|
123 |
for (String key: cacheRepository.keySet()) { |
|
124 |
LocalCacheData localCacheData = cacheRepository.get(key); |
|
125 |
if (localCacheData!=null && System.currentTimeMillis()>=localCacheData.getTimeoutTime()) { |
|
126 |
cacheRepository.remove(key); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
return true; |
|
131 |
} |
|
132 |
|
|
133 |
} |