小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            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;
                  }
          }

          另外一個類:
          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 小菜毛毛 閱讀(3698) 評論(0)  編輯  收藏 所屬分類: java基礎及其原理
          主站蜘蛛池模板: 灵台县| 宁武县| 卢龙县| 乐亭县| 馆陶县| 和顺县| 宜良县| 准格尔旗| 阜城县| 江安县| 额济纳旗| 宝兴县| 环江| 进贤县| 晋中市| 定远县| 高陵县| 宁蒗| 五原县| 沾益县| 澄城县| 儋州市| 裕民县| 肃宁县| 永靖县| 定结县| 朝阳区| 神农架林区| 华阴市| 平原县| 康保县| 中山市| 扶沟县| 特克斯县| 广元市| 和静县| 稷山县| 铜陵市| 当阳市| 万宁市| 盐源县|