自定義快取類
阿新 • • 發佈:2019-05-05
public class CacheMap<K, V> extends AbstractMap<K, V>{
//預設儲存時間
private static final long DEFAULT_TIMEOUT = 1000*60*10;
private static CacheMap<Object, Object> defaultInstance;
//快取時間
private long cacheTimeout;
private Map<K, CacheEntry> map = new HashMap<K, CacheEntry>();
public CacheMap(long timeout) {
this.cacheTimeout = timeout;
new ClearThread().start();
}
public static synchronized final CacheMap<Object, Object> getDefault() {
if (defaultInstance == null) {
defaultInstance = new CacheMap<Object, Object>(DEFAULT_TIMEOUT);
}
return defaultInstance;
}
//快取實體
private class CacheEntry implements Entry<K, V> {
long time;
V value;
K key;
CacheEntry(K key, V value) {
super();
this.value = value;
this.key = key;
this.time = System.currentTimeMillis();
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
return this.value = value;
}
}
//清理快取執行緒
private class ClearThread extends Thread {
ClearThread() {
setName("clear cache thread");
}
public void run() {
while (true) {
try {
long now = System.currentTimeMillis();
Object[] keys = map.keySet().toArray();
for (Object key : keys) {
CacheEntry entry = map.get(key);
if (now - entry.time >= cacheTimeout) {
synchronized (map) {
map.remove(key);
}
}
}
Thread.sleep(cacheTimeout);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public Set<Entry<K, V>> entrySet() {
Set<Entry<K, V>> entrySet = new HashSet<Entry<K, V>>();
Set<Entry<K, CacheEntry>> wrapEntrySet = map.entrySet();
for (Entry<K, CacheEntry> entry : wrapEntrySet) {
entrySet.add(entry.getValue());
}
return entrySet;
}
@Override
public V get(Object key) {
CacheEntry entry = map.get(key);
return entry == null ? null : entry.value;
}
@Override
public V put(K key, V value) {
CacheEntry entry = new CacheEntry(key, value);
synchronized (map) {
map.put(key, entry);
}
return valu