zoukankan      html  css  js  c++  java
  • 可定制生命周期的缓存

    可定制生命周期的缓存

    1) ICache.java 缓存接口

      1 package com.jgao.cache;
      2 
      3 /**
      4  * 缓存接口
      5  * @author jgao
      6  *
      7  */
      8 public interface ICache {
      9     
     10     public static int Forever = -1; //缓存中对象生命周期的结束标志
     11 
     12     /**
     13      * 判断缓存中的对象是否存在
     14      * @param key
     15      * @return
     16      */
     17     boolean contains(String key);
     18     
     19     /**
     20      * 获取缓存中的对象
     21      * @param key 对象名称
     22      * @return
     23      */
     24     Object get(String key);
     25 
     26     /**
     27      * 向缓存中插入对象
     28      * @param key 对象名称
     29      * @param obj 对象
     30      * @param slidingExpiration 对象在缓存中存在的时间
     31      */
     32     void Insert(String key, Object obj, int slidingExpiration);
     33 
     34     /**
     35      * 
     36      * 向缓存中添加对象,并返回该对象
     37      * @param key 对象名称
     38      * @param obj 对象
     39      * @param slidingExpiration 对象在缓存中存在的时间
     40      * @return
     41      */
     42     Object Add(String key, Object obj, int slidingExpiration);
     43     
     44     /**
     45      * 移除缓存中的对象
     46      * @param key 对象名称
     47      * @return
     48      */
     49     Object Remove(String key);
     50 }
     51 
     52 2) DefaultCache.java 默认缓存
     53 
     54 package com.jgao.cache;
     55 
     56 import java.util.*;
     57 
     58 /**
     59  * 系统默认的缓存
     60  * @author jgao
     61  *
     62  */
     63 class DefaultCache implements ICache {
     64 
     65     static int FreshTimerIntervalSeconds = 1; //缓存中对象生命周期的频率(一秒)
     66     Map<String, SimpleCacheInfo> datas; //缓存容器
     67     private Timer timer; //时间任务
     68     
     69     /**
     70      * 默认构造函数
     71      *
     72      */
     73     public DefaultCache() {
     74         //实例化有防止线程同步操作的缓存容器
     75         datas = Collections.synchronizedMap(new HashMap<String, SimpleCacheInfo>());
     76         
     77         //刷新缓存
     78         TimerTask task = new CacheFreshTask(this);
     79         timer = new Timer("SimpleCache_Timer", true);
     80         timer.scheduleAtFixedRate(task, 1000, FreshTimerIntervalSeconds * 1000);//每格一秒刷新一次(缓存中对象的生命周期减一)
     81     }
     82 
     83     /**
     84      * 判断缓存中的对象是否存在
     85      * @param key
     86      * @return
     87      */
     88     public boolean contains(String key){
     89         return datas.containsKey(key);
     90     }
     91     
     92     /**
     93      * 获取缓存中的对象
     94      * @param key 对象名称
     95      * @return
     96      */
     97     public Object get(String key) {
     98         if (datas.containsKey(key)) {
     99             SimpleCacheInfo sci = (SimpleCacheInfo)datas.get(key);
    100             //sci.setSecondsRemain(sci.getSecondsTotal());
    101             return sci.getObj();
    102         }
    103         return null;
    104     }
    105     
    106     /**
    107      * 向缓存中插入对象
    108      * @param key 对象名称
    109      * @param obj 对象
    110      * @param cacheSeconds 对象在缓存中存在的时间
    111      */
    112     public void Insert(String key, Object obj, int cacheSeconds) {
    113         Add(key, obj, cacheSeconds);
    114     }
    115     
    116     /**
    117      * 
    118      * 向缓存中添加对象,并返回该对象
    119      * @param key 对象名称
    120      * @param obj 对象
    121      * @param cacheSeconds 对象在缓存中存在的时间
    122      * @return
    123      */
    124     public Object Add(String key, Object obj, int cacheSeconds) {
    125         if (cacheSeconds != 0) {
    126             SimpleCacheInfo sci = new SimpleCacheInfo(obj, cacheSeconds);
    127             datas.put(key, sci);
    128         }
    129         return obj;
    130     }
    131     
    132     /**
    133      * 移除缓存中的对象
    134      * @param key 对象名称
    135      * @return
    136      */
    137     public Object Remove(String key) {
    138         SimpleCacheInfo sci = datas.remove(key);
    139         if (sci != null) {
    140             return sci.getObj();
    141         }
    142         return null;
    143     }
    144     
    145     /**
    146      * 缓存信息类(存储缓存中的对象和缓存时间)
    147      * @author jgao
    148      *
    149      */
    150     class SimpleCacheInfo {
    151         private Object obj;
    152         private int secondsRemain;
    153         private int cacheSeconds;
    154         
    155         public SimpleCacheInfo(Object obj, int cacheSeconds) {
    156             this.obj = obj;
    157             this.secondsRemain = cacheSeconds;
    158             this.cacheSeconds = cacheSeconds;
    159         }
    160         
    161         public Object getObj() {
    162             return obj;
    163         }
    164 
    165         int getSecondsTotal() {
    166             return cacheSeconds;
    167         }
    168         
    169         int getSecondsRemain() {
    170             return secondsRemain;
    171         }
    172         
    173         void setSecondsRemain(int value) {
    174             secondsRemain = value;
    175         }
    176     }
    177     
    178     /**
    179      * 管理缓存中对象的生命周期的任务类(用于定时刷新缓存中的对象)
    180      * @author jgao
    181      *
    182      */
    183     class CacheFreshTask extends TimerTask {
    184         private DefaultCache cache;
    185         public CacheFreshTask(DefaultCache cache) {
    186             this.cache = cache;
    187         }
    188 
    189         public void run() {
    190             synchronized (cache.datas) {
    191                 Iterator<Map.Entry<String, SimpleCacheInfo>> iterator
    192                     = cache.datas.entrySet().iterator();
    193                 while (iterator.hasNext()) {
    194                     Map.Entry<String, SimpleCacheInfo> entry = iterator.next();
    195                     SimpleCacheInfo sci = entry.getValue();
    196                     if (sci.getSecondsTotal() != ICache.Forever) {
    197                         sci.setSecondsRemain(sci.getSecondsRemain() - FreshTimerIntervalSeconds);
    198                         if (sci.getSecondsRemain() <= 0) {
    199                             iterator.remove();
    200                         }
    201                     }
    202                 }
    203             }
    204         }
    205     }
    206 }
    207 
    208 
    209 3) CacheFactory.java 缓存工厂
    210 
    211 package com.jgao.cache;
    212 
    213 /**
    214  * 缓存工厂,用于获取和制造缓存
    215  * @author jgao
    216  *
    217  */
    218 public class CacheFactory {
    219 
    220     private static ICache cache = null;
    221     
    222     /**
    223      * 获取caches指定的缓存
    224      * @param caches
    225      * @return 
    226      */
    227     public static ICache getCacheInstance(Class caches){
    228         if(cache==null){
    229             try {
    230                 cache = (ICache) caches.newInstance();
    231             } catch (InstantiationException e) {
    232                 System.out.println("指定的缓存类有误,caches参数必须是ICache的实现类");
    233                 e.printStackTrace();
    234             } catch (IllegalAccessException e) {
    235                 System.out.println("指定的缓存类有误,caches参数必须是ICache的实现类");
    236                 e.printStackTrace();
    237             }
    238         }
    239         return cache;
    240     }
    241     
    242     /**
    243      * 获取系统默认的缓存
    244      * @return
    245      */
    246     public static ICache getDefaultCache(){
    247         if(cache==null){
    248             cache = new DefaultCache();
    249         }else if(!(cache instanceof DefaultCache)){
    250             cache = new DefaultCache();
    251         }
    252         return cache;
    253     }
    254     
    255     public static void main(String[] args) {
    256         ICache cache = CacheFactory.getDefaultCache();
    257         if(cache.contains("area")){
    258             System.out.println(cache.get("area"));
    259         }else{
    260             cache.Insert("area","福州",120);
    261         }
    262         
    263     }
    264 }

    原帖地址:http://www.blogjava.net/jgao/archive/2007/04/22/112541.html

  • 相关阅读:
    《JAVA设计模式》之模板模式(Template)
    《JAVA设计模式》之策略模式(Strategy)
    《JAVA设计模式》之享元模式(Flyweight)
    《JAVA设计模式》之桥接模式(Bridge)
    《JAVA设计模式》之组合模式(Composite)
    《JAVA设计模式》之外观模式(Facade)
    《JAVA设计模式》之代理模式(Proxy)
    《JAVA设计模式》之装饰模式(Decorator)
    《JAVA设计模式》之适配器模式(Adapter)
    《JAVA设计模式》之原型模式(Prototype)
  • 原文地址:https://www.cnblogs.com/Overbord/p/2859550.html
Copyright © 2011-2022 走看看