<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//緩存中對象生命周期的結(jié)束標志

        
    /**
         * 判斷緩存中的對象是否存在
         * 
    @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.*;

    /**
     * 系統(tǒng)默認的緩存
     * 
    @author jgao
     *
     
    */

    class DefaultCache implements ICache {

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

        
    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;
            }

        }

        
        
    /**
         * 管理緩存中對象的生命周期的任務(wù)類(用于定時刷新緩存中的對象)
         * 
    @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參數(shù)必須是ICache的實現(xiàn)類");
                    e.printStackTrace();
                }
     catch (IllegalAccessException e) {
                    System.out.println(
    "指定的緩存類有誤,caches參數(shù)必須是ICache的實現(xiàn)類");
                    e.printStackTrace();
                }

            }

            
    return cache;
        }

        
        
    /**
         * 獲取系統(tǒng)默認的緩存
         * 
    @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編程隨筆 閱讀(1008) 評論(0)  編輯  收藏


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


    網(wǎng)站導(dǎo)航:
     
    <2007年4月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日本高清高色视频免费 | 麻豆69堂免费视频| 亚洲日本国产综合高清| 久久久久久亚洲Av无码精品专口| 亚洲欧洲精品无码AV| 国产亚洲综合一区柠檬导航| 区久久AAA片69亚洲| 亚洲无av在线中文字幕| 亚洲精品蜜桃久久久久久| 国产亚洲午夜高清国产拍精品| 免费人成视频x8x8入口| mm1313亚洲国产精品美女| 亚洲精品一级无码中文字幕| 精品国产人成亚洲区| 亚洲精品无码久久千人斩| 久久亚洲精品中文字幕无码| 久久水蜜桃亚洲av无码精品麻豆| 78成人精品电影在线播放日韩精品电影一区亚洲| 久久夜色精品国产嚕嚕亚洲av| 亚洲欧洲日韩不卡| 亚洲中文字幕人成乱码 | 亚洲午夜福利精品久久| 亚洲日韩精品无码一区二区三区| 久久精品国产亚洲综合色| 亚洲视频网站在线观看| 国产成人精品日本亚洲直接| 亚洲av无码成人精品区一本二本 | 亚洲伊人久久大香线蕉啊| 久久精品国产亚洲AV久| 亚洲av综合av一区二区三区 | 亚洲欧洲久久精品| 亚洲精品乱码久久久久久蜜桃图片 | 在线观看成人免费视频| mm1313亚洲精品国产| 亚洲高清视频在线观看| 亚洲AV无码成人专区| 国产精品亚洲va在线观看| 中国一级特黄高清免费的大片中国一级黄色片 | 久久w5ww成w人免费| 成全高清视频免费观看| 亚洲精品第一国产综合精品99|