timer实现定时的缓存器

  1. public class CacheTimerHandler {  html

  2.     private static final long SECOND_TIME = 1000;//默认过时时间 20秒  java

  3.     private static final int DEFUALT_VALIDITY_TIME = 20;//默认过时时间 20秒  缓存

  4.     private static final Timer timer ;  ide

  5.     private static final SimpleConcurrentMap<String, CacheEntity> map;  性能

  6.       

  7.     static{  this

  8.         timer = new Timer();  spa

  9.         map = new SimpleConcurrentMap<String, CacheEntity>(new HashMap<String, CacheEntity>(1<<18));  .net

  10.     }  线程

  11.               

  12.     /** htm

  13.      * 增长缓存对象 

  14.      * @param key 

  15.      * @param ce 

  16.      */  

  17.     public static void addCache(String key, CacheEntity ce){  

  18.         addCache(key, ce, DEFUALT_VALIDITY_TIME);  

  19.     }  

  20.       

  21.     /** 

  22.      * 增长缓存对象 

  23.      * @param key 

  24.      * @param ce 

  25.      * @param validityTime 有效时间 

  26.      */  

  27.     public static synchronized void addCache(String key, CacheEntity ce, int validityTime){  

  28.         map.put(key, ce);  

  29.         //添加过时定时  

  30.         timer.schedule(new TimeoutTimerTask(key), validityTime * SECOND_TIME);  

  31.     }  

  32.       

  33.     /** 

  34.      * 获取缓存对象 

  35.      * @param key 

  36.      * @return 

  37.      */  

  38.     public static synchronized CacheEntity getCache(String key){  

  39.         return map.get(key);  

  40.     }  

  41.       

  42.     /** 

  43.      * 检查是否含有制定key的缓冲 

  44.      * @param key 

  45.      * @return 

  46.      */  

  47.     public static synchronized boolean isConcurrent(String key){  

  48.         return map.containsKey(key);  

  49.     }  

  50.       

  51.     /** 

  52.      * 删除缓存 

  53.      * @param key 

  54.      */  

  55.     public static synchronized void removeCache(String key){  

  56.         map.remove(key);  

  57.     }  

  58.       

  59.     /** 

  60.      * 获取缓存大小 

  61.      * @param key 

  62.      */  

  63.     public static int getCacheSize(){  

  64.         return map.size();  

  65.     }  

  66.       

  67.     /** 

  68.      * 清除所有缓存 

  69.      */  

  70.     public static synchronized void clearCache(){  

  71.         if(null != timer){  

  72.             timer.cancel();  

  73.         }  

  74.         map.clear();  

  75.         System.out.println("clear cache");  

  76.     }  

  77.       


  78.     static class TimeoutTimerTask extends TimerTask{  

  79.         private String ceKey ;  

  80.           

  81.         public TimeoutTimerTask(String key){  

  82.             this.ceKey = key;  

  83.         }  

  84.   

  85.         @Override  

  86.         public void run() {  

  87.             CacheTimerHandler.removeCache(ceKey);  

  88.             System.out.println("remove : "+ceKey);  

  89.         }  

  90.     }  

  91. }  


timer方式有点是适用性更强,由于每一个缓存的过时时间均可以独立配置的;ist只能适用 于缓存时间都同样的线性过时。从性能开销方面,由于timer是与缓存对象数量成正比的,在缓存量很大的时候,在缓存时间内系统开销也随之提升;而 list方式只要一个线程管理过时清理就能够了。ReadWriteLock 请参考http://www.blogjava.net/xylz/archive/2010/07/14/326080.html




在这里程序还有更好的方式来实现

使用FutureTask来获取任务,这样避免了每次执行相同计算工做,例如 

一个线程在执行map.put(5),此时第二个线程也是执行map.put(5),或者执行一系列的计算,目的是get(5)这个操做,但是这个操做已经在进行了,如今咱们须要一种先进行判断的模式,先判断是否有线程正在执行这个操做,若是有咱们就不在执行,这样能够避免重覆的开销,有线程在执行这个操做那就继续等待其执行完,而不用再去执行一次

相关文章
相关标签/搜索