java 实现cache

lliyuqiu2 贡献于2012-07-02

作者 微软中国  创建于2011-07-20 15:53:00   修改者微软中国  修改于2011-07-20 15:53:00字数1806

文档摘要:在开发项目工程时,经常会遇到保存某些值放到系统的cache中,现用Cache.java和CacheManager.java来管理。
关键词:

在开发项目工程时,经常会遇到保存某些值放到系统的cache中,现用Cache.java和CacheManager.java来管理。具体代码分别如下: 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();                 private CacheManager() {                 super();         }                 private synchronized static Cache getCache(String key) {                 return (Cache)cacheMap.get(key);         }                 private synchronized static boolean hasCache(String key) {                 return cacheMap.containsKey(key);         }                 public synchronized static void invalidateAll() {                 cacheMap.clear();         }                 public synchronized static void invalidate(String key) {                 cacheMap.remove(key);         }                 private synchronized static void putCache(String key, Cache object) {            cacheMap.put(key, object);         }                 public static Cache getContent(String key) {                  if (hasCache(key)) {                         Cache cache = getCache(key);                         if (cacheExpired(cache)) {                                 cache.setExpired(true);                         }                         return cache;                  } else {                          return null;                  }         }                 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);         }                          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;                 }         } }  

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档