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

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

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

    小菜毛毛技術(shù)分享

    與大家共同成長

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
    public class Cache {
            private String key;
            private Object value;
            private long timeOut;
            private boolean expired;

            public Cache() {
                    super();
            }
                    
            public Cache(String key, String value, long timeOut, boolean expired) {
                    this.key = key;
                    this.value = value;
                    this.timeOut = timeOut;
                    this.expired = expired;
            }

            public String getKey() {
                    return key;
            }

            public long getTimeOut() {
                    return timeOut;
            }

            public Object getValue() {
                    return value;
            }

            public void setKey(String string) {
                    key = string;
            }

            public void setTimeOut(long l) {
                    timeOut = l;
            }

            public void setValue(Object object) {
                    value = object;
            }

            public boolean isExpired() {
                    return expired;
            }

            public void setExpired(boolean b) {
                    expired = b;
            }
    }

    另外一個(gè)類:
    import java.util.Date;
    import java.util.HashMap;

    public class CacheManager {
            private static HashMap cacheMap = new HashMap();

            /**
             * This class is singleton so private constructor is used.
             */
            private CacheManager() {
                    super();
            }

            /**
             * returns cache item from hashmap
             * @param key
             * @return Cache
             */
            private synchronized static Cache getCache(String key) {
                    return (Cache)cacheMap.get(key);
            }

            /**
             * Looks at the hashmap if a cache item exists or not
             * @param key
             * @return Cache
             */
            private synchronized static boolean hasCache(String key) {
                    return cacheMap.containsKey(key);
            }

            /**
             * Invalidates all cache
             */
            public synchronized static void invalidateAll() {
                    cacheMap.clear();
            }

            /**
             * Invalidates a single cache item
             * @param key
             */
            public synchronized static void invalidate(String key) {
                    cacheMap.remove(key);
            }

            /**
             * Adds new item to cache hashmap
             * @param key
             * @return Cache
             */
            private synchronized static void putCache(String key, Cache object) {
               cacheMap.put(key, object);
            }

            /**
             * Reads a cache item's content
             * @param key
             * @return
             */
            public static Cache getContent(String key) {
                     if (hasCache(key)) {
                            Cache cache = getCache(key);
                            if (cacheExpired(cache)) {
                                    cache.setExpired(true);
                            }
                            return cache;
                     } else {
                             return null;
                     }
            }

            /**
             * 
             * @param key
             * @param content
             * @param ttl
             */
            public static void putContent(String key, Object content, long ttl) {
                    Cache cache = new Cache();
                    cache.setKey(key);
                    cache.setValue(content);
                    cache.setTimeOut(ttl + new Date().getTime());
                    cache.setExpired(false);
                    putCache(key, cache);
            }
            
            /** @modelguid {172828D6-3AB2-46C4-96E2-E72B34264031} */
            private static boolean cacheExpired(Cache cache) {
                    if (cache == null) {
                            return false;
                    }
                    long milisNow = new Date().getTime();
                    long milisExpire = cache.getTimeOut();
                    if (milisExpire < 0) {                // Cache never expires 
                            return false;
                    } else if (milisNow >= milisExpire) {
                            return true;
                    } else {
                            return false;
                    }
            }

    }

    posted on 2009-12-07 10:10 小菜毛毛 閱讀(3696) 評(píng)論(0)  編輯  收藏 所屬分類: java基礎(chǔ)及其原理
    主站蜘蛛池模板: 中文字幕亚洲天堂| 亚洲精品专区在线观看| 老司机永久免费网站在线观看| 好爽…又高潮了免费毛片| 日韩视频在线免费| 九月婷婷亚洲综合在线| 人人狠狠综合久久亚洲婷婷| 亚洲人成电影福利在线播放| 亚洲资源最新版在线观看| 亚洲成av人片在线天堂无| 一级毛片人与动免费观看| 男人进去女人爽免费视频国产 | 亚洲一卡二卡三卡| 亚洲风情亚Aⅴ在线发布| 成年免费a级毛片| 香港a毛片免费观看| 亚洲免费视频网址| 国产成人免费全部网站| 国产亚洲人成网站在线观看| 78成人精品电影在线播放日韩精品电影一区亚洲 | 美女露隐私全部免费直播| 免费人成激情视频在线观看冫| 免费观看无遮挡www的视频| 日本成人免费在线| 亚洲色精品88色婷婷七月丁香| 亚洲一级免费毛片| 一级看片免费视频| 亚洲一区免费视频| 免费一看一级毛片人| 亚洲国产精品婷婷久久| 午夜亚洲国产理论片二级港台二级| 狠狠躁狠狠爱免费视频无码| 无码乱肉视频免费大全合集| 国产偷国产偷亚洲高清日韩| 亚洲国产品综合人成综合网站| 成年大片免费高清在线看黄| 台湾一级毛片永久免费| 国产亚洲精久久久久久无码77777| 亚洲jjzzjjzz在线观看| A毛片毛片看免费| 日本特黄特黄刺激大片免费|