<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    可定制生命周期的緩存

    1) ICache.java 緩存接口

    package com.jgao.cache;

    /**
     * 緩存接口
     * 
    @author jgao
     *
     
    */

    public interface ICache {
        
        
    public static int Forever = -1//緩存中對象生命周期的結束標志

        
    /**
         * 判斷緩存中的對象是否存在
         * 
    @param key
         * 
    @return
         
    */

        
    boolean contains(String key);
        
        
    /**
         * 獲取緩存中的對象
         * 
    @param key 對象名稱
         * 
    @return
         
    */

        Object get(String key);

        
    /**
         * 向緩存中插入對象
         * 
    @param key 對象名稱
         * 
    @param obj 對象
         * 
    @param slidingExpiration 對象在緩存中存在的時間
         
    */

        
    void Insert(String key, Object obj, int slidingExpiration);

        
    /**
         * 
         * 向緩存中添加對象,并返回該對象
         * 
    @param key 對象名稱
         * 
    @param obj 對象
         * 
    @param slidingExpiration 對象在緩存中存在的時間
         * 
    @return
         
    */

        Object Add(String key, Object obj, 
    int slidingExpiration);
        
        
    /**
         * 移除緩存中的對象
         * 
    @param key 對象名稱
         * 
    @return
         
    */

        Object Remove(String key);
    }

    2) DefaultCache.java 默認緩存
    package com.jgao.cache;

    import java.util.*;

    /**
     * 系統默認的緩存
     * 
    @author jgao
     *
     
    */

    class DefaultCache implements ICache {

        
    static int FreshTimerIntervalSeconds = 1//緩存中對象生命周期的頻率(一秒)
        Map<String, SimpleCacheInfo> datas; //緩存容器
        private Timer timer; //時間任務
        
        
    /**
         * 默認構造函數
         *
         
    */

        
    public DefaultCache() {
            
    //實例化有防止線程同步操作的緩存容器
            datas = Collections.synchronizedMap(new HashMap<String, SimpleCacheInfo>());
            
            
    //刷新緩存
            TimerTask task = new CacheFreshTask(this);
            timer 
    = new Timer("SimpleCache_Timer"true);
            timer.scheduleAtFixedRate(task, 
    1000, FreshTimerIntervalSeconds * 1000);//每格一秒刷新一次(緩存中對象的生命周期減一)
        }


        
    /**
         * 判斷緩存中的對象是否存在
         * 
    @param key
         * 
    @return
         
    */

        
    public boolean contains(String key){
            
    return datas.containsKey(key);
        }

        
        
    /**
         * 獲取緩存中的對象
         * 
    @param key 對象名稱
         * 
    @return
         
    */

        
    public Object get(String key) {
            
    if (datas.containsKey(key)) {
                SimpleCacheInfo sci 
    = (SimpleCacheInfo)datas.get(key);
                
    //sci.setSecondsRemain(sci.getSecondsTotal());
                return sci.getObj();
            }

            
    return null;
        }

        
        
    /**
         * 向緩存中插入對象
         * 
    @param key 對象名稱
         * 
    @param obj 對象
         * 
    @param cacheSeconds 對象在緩存中存在的時間
         
    */

        
    public void Insert(String key, Object obj, int cacheSeconds) {
            Add(key, obj, cacheSeconds);
        }

        
        
    /**
         * 
         * 向緩存中添加對象,并返回該對象
         * 
    @param key 對象名稱
         * 
    @param obj 對象
         * 
    @param cacheSeconds 對象在緩存中存在的時間
         * 
    @return
         
    */

        
    public Object Add(String key, Object obj, int cacheSeconds) {
            
    if (cacheSeconds != 0{
                SimpleCacheInfo sci 
    = new SimpleCacheInfo(obj, cacheSeconds);
                datas.put(key, sci);
            }

            
    return obj;
        }

        
        
    /**
         * 移除緩存中的對象
         * 
    @param key 對象名稱
         * 
    @return
         
    */

        
    public Object Remove(String key) {
            SimpleCacheInfo sci 
    = datas.remove(key);
            
    if (sci != null{
                
    return sci.getObj();
            }

            
    return null;
        }

        
        
    /**
         * 緩存信息類(存儲緩存中的對象和緩存時間)
         * 
    @author jgao
         *
         
    */

        
    class SimpleCacheInfo {
            
    private Object obj;
            
    private int secondsRemain;
            
    private int cacheSeconds;
            
            
    public SimpleCacheInfo(Object obj, int cacheSeconds) {
                
    this.obj = obj;
                
    this.secondsRemain = cacheSeconds;
                
    this.cacheSeconds = cacheSeconds;
            }

            
            
    public Object getObj() {
                
    return obj;
            }


            
    int getSecondsTotal() {
                
    return cacheSeconds;
            }

            
            
    int getSecondsRemain() {
                
    return secondsRemain;
            }

            
            
    void setSecondsRemain(int value) {
                secondsRemain 
    = value;
            }

        }

        
        
    /**
         * 管理緩存中對象的生命周期的任務類(用于定時刷新緩存中的對象)
         * 
    @author jgao
         *
         
    */

        
    class CacheFreshTask extends TimerTask {
            
    private DefaultCache cache;
            
    public CacheFreshTask(DefaultCache cache) {
                
    this.cache = cache;
            }


            
    public void run() {
                
    synchronized (cache.datas) {
                    Iterator
    <Map.Entry<String, SimpleCacheInfo>> iterator
                        
    = cache.datas.entrySet().iterator();
                    
    while (iterator.hasNext()) {
                        Map.Entry
    <String, SimpleCacheInfo> entry = iterator.next();
                        SimpleCacheInfo sci 
    = entry.getValue();
                        
    if (sci.getSecondsTotal() != ICache.Forever) {
                            sci.setSecondsRemain(sci.getSecondsRemain() 
    - FreshTimerIntervalSeconds);
                            
    if (sci.getSecondsRemain() <= 0{
                                iterator.remove();
                            }

                        }

                    }

                }

            }

        }

    }


    3) CacheFactory.java 緩存工廠
    package com.jgao.cache;

    /**
     * 緩存工廠,用于獲取和制造緩存
     * 
    @author jgao
     *
     
    */

    public class CacheFactory {

        
    private static ICache cache = null;
        
        
    /**
         * 獲取caches指定的緩存
         * 
    @param caches
         * 
    @return 
         
    */

        
    public static ICache getCacheInstance(Class caches){
            
    if(cache==null){
                
    try {
                    cache 
    = (ICache) caches.newInstance();
                }
     catch (InstantiationException e) {
                    System.out.println(
    "指定的緩存類有誤,caches參數必須是ICache的實現類");
                    e.printStackTrace();
                }
     catch (IllegalAccessException e) {
                    System.out.println(
    "指定的緩存類有誤,caches參數必須是ICache的實現類");
                    e.printStackTrace();
                }

            }

            
    return cache;
        }

        
        
    /**
         * 獲取系統默認的緩存
         * 
    @return
         
    */

        
    public static ICache getDefaultCache(){
            
    if(cache==null){
                cache 
    = new DefaultCache();
            }
    else if(!(cache instanceof DefaultCache)){
                cache 
    = new DefaultCache();
            }

            
    return cache;
        }

        
        
    public static void main(String[] args) {
            ICache cache 
    = CacheFactory.getDefaultCache();
            
    if(cache.contains("area")){
                System.out.println(cache.get(
    "area"));
            }
    else{
                cache.Insert(
    "area","福州",120);
            }

            
        }

    }

    posted on 2007-04-22 06:18 JGAO編程隨筆 閱讀(1007) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2007年4月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人精品亚洲人成在线| 亚洲AV无码码潮喷在线观看| 亚洲白色白色在线播放| 最近免费中文字幕MV在线视频3| 亚洲乱码日产精品a级毛片久久| 国产精品亚洲专区无码牛牛| 最近2019中文字幕mv免费看 | 精品久久8x国产免费观看| 亚洲精品无码专区久久久| 国产免费福利体检区久久| 日日噜噜噜噜夜夜爽亚洲精品| 国产在线精品观看免费观看| 亚洲国产精品无码久久久秋霞2 | 亚洲国产天堂久久综合| 国产午夜无码片免费| 亚洲AV永久无码精品水牛影视| 无码专区AAAAAA免费视频| 亚洲综合在线成人一区| 天天摸天天碰成人免费视频| 亚洲hairy多毛pics大全| 国产精品亚洲视频| 亚洲一区免费视频| 亚洲AV无码成人网站在线观看| 亚洲高清最新av网站| 嫩草在线视频www免费观看| 亚洲高清美女一区二区三区| 日韩午夜免费视频| 精品国产呦系列在线观看免费| 亚洲国产成人久久综合一| 成年人免费网站在线观看| 一级成人a做片免费| 亚洲avav天堂av在线不卡| 四虎免费在线观看| 成人毛片100免费观看| 亚洲精品在线电影| 国产麻豆免费观看91| 热99RE久久精品这里都是精品免费| 亚洲综合色一区二区三区小说| 日本特黄特色免费大片| 成人免费区一区二区三区| 亚洲乱码中文字幕在线|