可以实现不等待,线程自动更新缓存
java动态缓jar包http://dl2.youkuaiyun.com/down4/20070918/18203740920.jar
源代码:
CacheData.java 存放缓存数据的Bean
/** */
/**
*
*/
package
com.cari.web.cache;


/** */
/**
* @author zsy
*
*/

public
class
CacheData
...
{
private Object data;
private long time;
private int count;

public CacheData() ...{
}

public CacheData(Object data, long time, int count) ...{
this.data = data;
this.time = time;
this.count = count;
}

public CacheData(Object data) ...{
this.data = data;
this.time = System.currentTimeMillis();
this.count = 1;
}

public void addCount() ...{
count++;
}

public int getCount() ...{
return count;
}

public void setCount(int count) ...{
this.count = count;
}

public Object getData() ...{
return data;
}

public void setData(Object data) ...{
this.data = data;
}

public long getTime() ...{
return time;
}

public void setTime(long time) ...{
this.time = time;
}
}
CacheOperation.java 缓存处理类
package
com.cari.web.cache;

import
java.lang.reflect.Method;
import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.Hashtable;

import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;


/** */
/**
* @author zsy
*/

public
class
CacheOperation
...
{
private static final Log log = LogFactory.getLog(CacheOperation.class);
private static CacheOperation singleton = null;
private Hashtable cacheMap;//存放缓存数据
private ArrayList threadKeys;//处于线程更新中的key值列表

public static CacheOperation getInstance() ...{

if (singleton == null) ...{
singleton = new CacheOperation();
}
return singleton;
}

private CacheOperation() ...{
cacheMap = new Hashtable();
threadKeys = new ArrayList();
}

/** *//**
* 添加数据缓存
* 与方法getCacheData(String key, long intervalTime, int maxVisitCount)配合使用
* @param key
* @param data
*/

public void addCacheData(String key, Object data) ...{
addCacheData(key, data, true);
}

private void addCacheData(String key, Object data, boolean check) ...{

if (Runtime.getRuntime().freeMemory() < 5L*1024L*1024L) ...{//虚拟机内存小于10兆,则清除缓存
log.warn("WEB缓存:内存不足,开始清空缓存!");
removeAllCacheData();
return;

} else if(check && cacheMap.containsKey(key)) ...{
log.warn("WEB缓存:key值= " + key + " 在缓存中重复, 本次不缓存!");
return;
}
cacheMap.put(key, new CacheData(data));
}

/** *//**
* 取得缓存中的数据
* 与方法addCacheData(String key, Object data)配合使用
* @param key
* @param intervalTime 缓存的时间周期,小于等于0时不限制
* @param maxVisitCount 访问累积次数,小于等于0时不限制
* @return
*/

public Object getCacheData(String key, long intervalTime, int maxVisitCount) ...{
CacheData cacheData = (CacheData)cacheMap.get(key);

if (cacheData == null) ...{
return null;
}

if (intervalTime > 0 && (System.currentTimeMillis() - cacheData.getTime()) > intervalTime) ...{
removeCacheData(key);
return null;
}

if (maxVisitCount > 0 && (maxVisitCount - cacheData.getCount()) <= 0) ...{
removeCacheData(key);
return null;

} else ...{
cacheData.addCount();
}
return cacheData.getData();
}