java Map实现的cache manager,定时清除缓存起来的值

用来存储短暂对象的缓存类,实现Map接口,内部有一个定时器用来清除过期(30秒)的对象。 为避免创建过多线程,没有特殊要求请使用getDefault()方法来获取本类的实例。

[java]  view plain  copy
  1. package com.zuidaima.modules.common.util;  
  2.   
  3. import java.util.AbstractMap;  
  4. import java.util.HashMap;  
  5. import java.util.HashSet;  
  6. import java.util.Map;  
  7. import java.util.Set;  
  8.   
  9. /** 
  10.  * 用来存储短暂对象的缓存类,实现Map接口,内部有一个定时器用来清除过期(30秒)的对象。 
  11.  * 为避免创建过多线程,没有特殊要求请使用getDefault()方法来获取本类的实例。 
  12.  * @author www.zuidaima.com 
  13.  * @param <K> 
  14.  * @param <V> 
  15.  */  
  16.   
  17. public class CacheMap<K, V> extends AbstractMap<K, V> {  
  18.   
  19.     private static final long DEFAULT_TIMEOUT = 30000;  
  20.     private static CacheMap<Object, Object> defaultInstance;  
  21.   
  22.     public static synchronized final CacheMap<Object, Object> getDefault() {  
  23.         if (defaultInstance == null) {  
  24.             defaultInstance = new CacheMap<Object, Object>(DEFAULT_TIMEOUT);  
  25.         }  
  26.         return defaultInstance;  
  27.     }  
  28.   
  29.     private class CacheEntry implements Entry<K, V> {  
  30.         long time;  
  31.         V value;  
  32.         K key;  
  33.   
  34.         CacheEntry(K key, V value) {  
  35.             super();  
  36.             this.value = value;  
  37.             this.key = key;  
  38.             this.time = System.currentTimeMillis();  
  39.         }  
  40.   
  41.         @Override  
  42.         public K getKey() {  
  43.             return key;  
  44.         }  
  45.   
  46.         @Override  
  47.         public V getValue() {  
  48.             return value;  
  49.         }  
  50.   
  51.         @Override  
  52.         public V setValue(V value) {  
  53.             return this.value = value;  
  54.         }  
  55.     }  
  56.   
  57.     private class ClearThread extends Thread {  
  58.         ClearThread() {  
  59.             setName("clear cache thread");  
  60.         }  
  61.   
  62.         public void run() {  
  63.             while (true) {  
  64.                 try {  
  65.                     long now = System.currentTimeMillis();  
  66.                     Object[] keys = map.keySet().toArray();  
  67.                     for (Object key : keys) {  
  68.                         CacheEntry entry = map.get(key);  
  69.                         if (now - entry.time >= cacheTimeout) {  
  70.                             synchronized (map) {  
  71.                                 map.remove(key);  
  72.                             }  
  73.                         }  
  74.                     }  
  75.                     Thread.sleep(cacheTimeout);  
  76.                 } catch (Exception e) {  
  77.                     e.printStackTrace();  
  78.                 }  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     private long cacheTimeout;  
  84.     private Map<K, CacheEntry> map = new HashMap<K, CacheEntry>();  
  85.   
  86.     public CacheMap(long timeout) {  
  87.         this.cacheTimeout = timeout;  
  88.         new ClearThread().start();  
  89.     }  
  90.   
  91.     @Override  
  92.     public Set<Entry<K, V>> entrySet() {  
  93.         Set<Entry<K, V>> entrySet = new HashSet<Map.Entry<K, V>>();  
  94.         Set<Entry<K, CacheEntry>> wrapEntrySet = map.entrySet();  
  95.         for (Entry<K, CacheEntry> entry : wrapEntrySet) {  
  96.             entrySet.add(entry.getValue());  
  97.         }  
  98.         return entrySet;  
  99.     }  
  100.   
  101.     @Override  
  102.     public V get(Object key) {  
  103.         CacheEntry entry = map.get(key);  
  104.         return entry == null ? null : entry.value;  
  105.     }  
  106.   
  107.     @Override  
  108.     public V put(K key, V value) {  
  109.         CacheEntry entry = new CacheEntry(key, value);  
  110.         synchronized (map) {  
  111.             map.put(key, entry);  
  112.         }  
  113.         return value;  
  114.     }  
  115.   
  116. }  

猜你喜欢

转载自blog.csdn.net/wsh_0703/article/details/80339718
今日推荐