創建一個靜態Hashtable用於保存key和value,對於cache過期後的方法回調,在cache過期後,再訪問cache的時候進行,避免了使用定時器輪詢過期時間,進行cache清除的效率損耗。
使用synchronized關鍵字進行多線程同步。
包括二個類和一個接口:
cache類:裏面都是靜態方法,提供基於key,value的方法進行cache的添加,修改,訪問,進行cache過期後調用callback方法。
cacheitem類:用於管理每個條目的cache內容和超時時間回調方法
ICacheMethod接口:cache到期回調方法需要實現的接口
cache類:裏面都是靜態方法
package org.netjframework.cache;
import java.util.Date;
public class Cache {
private static java.util.Hashtable<String,Object> __cacheList = new java.util.Hashtable<String,Object>();
public Cache() {
}
//添加cache,不過期
public synchronized static void add(String key, Object value) {
Cache.add(key, value, -1);
}
//添加cache有過期時間
public synchronized static void add(String key, Object value, long timeOut) {
Cache.add(key, value, timeOut, null);
}
//添加cache有過期時間並且具有回調方法
public synchronized static void add(String key, Object value, long timeOut,ICacheMethod callback) {
if (timeOut > 0) {
timeOut += new Date().getTime();
}
CacheItem item = new CacheItem(key, value, timeOut,callback);
Cache.__cacheList.put(key, item);
}
//獲取cache
public synchronized static Object get(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj == null) {
return null;
}
CacheItem item = (CacheItem) obj;
boolean expired = Cache.cacheExpired(key);
if (expired == true) // 已過期
{
if(item.getCallback()==null)
{
Cache.remove(key);
return null;
}
else
{
ICacheMethod callback=item.getCallback();
callback.execute(key);
expired = Cache.cacheExpired(key);
if(expired==true)
{
Cache.remove(key);
return null;
}
}
}
return item.getValue();
}
//移除cache
public synchronized static void remove(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj != null) {
obj = null;
}
Cache.__cacheList.remove(key);
}
//清理所有cache對象
public synchronized static void clear() {
for(String s:Cache.__cacheList.keySet())
{
Cache.__cacheList.put(s, null);
}
Cache.__cacheList.clear();
}
//判斷是否過期
private static boolean cacheExpired(String key) {
CacheItem item = (CacheItem) Cache.__cacheList.get(key);
if (item == null) {
return false;
}
long milisNow = new Date().getTime();
long milisExpire = item.getTimeOut();
if (milisExpire <= 0) { // 不過期
return false;
} else if (milisNow >= milisExpire) {
return true;
} else {
return false;
}
}
}
cacheitem類:用於管理每個條目的cache內容和超時時間回調方法
package org.netjframework.cache;
public class CacheItem {
private String key;
private Object value;
private long timeOut;
private ICacheMethod callback = null;
public CacheItem() {
}
public ICacheMethod getCallback() {
return callback;
}
public void setCallback(ICacheMethod callback) {
this.callback = callback;
}
public CacheItem(String key, Object value) {
this.key = key;
this.value = value;
this.timeOut = 0;
}
public CacheItem(String key, Object value, long timeOut) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
}
public CacheItem(String key, Object value, long timeOut,
ICacheMethod callback) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.callback = callback;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
}
ICacheMethod接口:cache到期回調方法需要實現的接口
package org.netjframework.cache;
public interface ICacheMethod {
public void execute(String key);
}