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

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

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

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

    與大家共同成長(zhǎng)

      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ǔ)及其原理
    主站蜘蛛池模板: 亚洲高清免费在线观看| 香港a毛片免费观看| 亚洲国产模特在线播放| 波多野结衣一区二区免费视频| 99热这里有免费国产精品| 亚洲日韩在线观看免费视频| 亚洲色无码专区一区| 久久精品国产亚洲av麻豆小说| 久久影院亚洲一区| 国产成人免费一区二区三区| 国产在线a免费观看| 最近高清中文字幕免费| a毛片视频免费观看影院| 日韩精品无码永久免费网站| 亚洲精品无码av中文字幕| 亚洲一级高清在线中文字幕| 亚洲精品福利网泷泽萝拉| 亚洲AV无码专区国产乱码电影 | 亚洲国产成人爱av在线播放| 成人免费一区二区无码视频| 亚洲一区二区三区免费在线观看 | 免费吃奶摸下激烈视频| 免费无码不卡视频在线观看| 成年人视频在线观看免费| 青青青国产在线观看免费| 日韩在线播放全免费| 四虎在线视频免费观看视频| 伊人久久免费视频| 一区二区三区在线免费看| 99热在线免费播放| 99爱在线精品视频免费观看9| 免费无遮挡无码永久视频| 老汉精品免费AV在线播放| 99爱视频99爱在线观看免费| 97公开免费视频| 日本视频一区在线观看免费| 91精品免费在线观看| 夫妻免费无码V看片| 国产精品国产免费无码专区不卡 | 亚洲成a人片在线观看中文app| 亚洲色成人网一二三区|