timer实现定时的缓存器

  1. public class CacheTimerHandler {  

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

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

  4.     private static final Timer timer ;  

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

  6.       

  7.     static{  

  8.         timer = new Timer();  

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

  10.     }  

  11.               

  12.     /** 

  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)这个操作,可是这个操作已经在进行了,现在我们需要一种先进行判断的模式,先判断是否有线程正在执行这个操作,如果有我们就不在执行,这样可以避免重覆的开销,有线程在执行这个操作那就继续等待其执行完,而不用再去执行一次
























转载于:https://my.oschina.net/u/876290/blog/373280

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值