<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 小菜毛毛 閱讀(3700) 評(píng)論(0)  編輯  收藏 所屬分類: java基礎(chǔ)及其原理
    主站蜘蛛池模板: 国产大陆亚洲精品国产| 亚洲国产精品成人精品无码区在线| 亚洲欧洲久久av| 免费成人在线电影| 国产亚洲中文日本不卡二区| a视频在线免费观看| 亚洲成av人片在线观看天堂无码| 激情亚洲一区国产精品| 37pao成人国产永久免费视频| 亚洲乱码日产一区三区| 丁香六月婷婷精品免费观看| 国产色婷婷精品免费视频| 亚洲中文无码亚洲人成影院| 亚洲免费人成在线视频观看| 国产精品观看在线亚洲人成网| 欧洲黑大粗无码免费| 亚洲国产成+人+综合| 中文字幕无码成人免费视频| 亚洲人成在线免费观看| 久久久久av无码免费网| 亚洲成aⅴ人片在线观| 57pao一国产成视频永久免费| 亚洲AV日韩AV永久无码免下载| 青青操视频在线免费观看| 精品亚洲综合在线第一区| 中文字幕无码免费久久| 亚洲VA中文字幕无码毛片| 无码一区二区三区免费| 亚洲综合激情九月婷婷| 国产人成免费视频网站| 亚洲伊人久久大香线蕉| 欧美好看的免费电影在线观看| 日韩亚洲国产高清免费视频| 成人免费男女视频网站慢动作| 亚洲国产精品成人AV在线| 免费日本黄色网址| 免费精品国偷自产在线在线| 亚洲精品乱码久久久久蜜桃| 四虎免费久久影院| 成人免费在线观看网站| 亚洲AV无码一区二区三区性色|