private CacheManager cacheManager;
private static Cache cache;
/**
* Add object to the specified cache
*/
public synchronized void addObject(String key, Object object) {
Element element = new Element(key,object);
cache.put(element);
}我这样使用Ehcache,从xml中读取数据,然后保存到cache中,我是像上面一样保存的,取得时候,是这样取的
public Object get(Class classObj, String nodeName, String fileName) {
Object obj = null;
if (ehcacheVindicator.getCache().isKeyInCache(nodeName)) {
Element element = ehcacheVindicator.getCache().get(nodeName);
if (ehcacheVindicator.getCache().isExpired(element))
obj = readObject(classObj, fileName, nodeName);// read object
// from xml
// file
else
obj = getObject(nodeName); // get object from cache
} else {
obj = readObject(classObj, fileName, nodeName); // read object from
// xml file
addObject(nodeName, obj); // add object to cache
}
return obj;
}也就是当不存在key时就从xml中取 ,但是这样好像没有效果,每次取的时候还是从xml中取,也就是
ehcacheVindicator.getCache().isKeyInCache(nodeName) 这句话总是返回false我遮掩测试了
Element element = new Element(key,object);
System.out.println(cache.getSize());
cache.put(element);
输出的总是0
第一次运行的时候应该是0 但是第二次测试的时候还是0 不知道为什么了?也就是cache运用压根就没效,哪位大哥可以帮我解决,我qq是874904507,会的麻烦告诉我一声
private static Cache cache;
/**
* Add object to the specified cache
*/
public synchronized void addObject(String key, Object object) {
Element element = new Element(key,object);
cache.put(element);
}我这样使用Ehcache,从xml中读取数据,然后保存到cache中,我是像上面一样保存的,取得时候,是这样取的
public Object get(Class classObj, String nodeName, String fileName) {
Object obj = null;
if (ehcacheVindicator.getCache().isKeyInCache(nodeName)) {
Element element = ehcacheVindicator.getCache().get(nodeName);
if (ehcacheVindicator.getCache().isExpired(element))
obj = readObject(classObj, fileName, nodeName);// read object
// from xml
// file
else
obj = getObject(nodeName); // get object from cache
} else {
obj = readObject(classObj, fileName, nodeName); // read object from
// xml file
addObject(nodeName, obj); // add object to cache
}
return obj;
}也就是当不存在key时就从xml中取 ,但是这样好像没有效果,每次取的时候还是从xml中取,也就是
ehcacheVindicator.getCache().isKeyInCache(nodeName) 这句话总是返回false我遮掩测试了
Element element = new Element(key,object);
System.out.println(cache.getSize());
cache.put(element);
输出的总是0
第一次运行的时候应该是0 但是第二次测试的时候还是0 不知道为什么了?也就是cache运用压根就没效,哪位大哥可以帮我解决,我qq是874904507,会的麻烦告诉我一声
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.RegisteredEventListeners;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy.MemoryStoreEvictionPolicyEnum;public class CacheTest {
public static void main(String[] args) {
CacheManager cacheManager = CacheManager.create();
String name = "idcache";
int maxElementsInMemory = 100000;
MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LRU;
boolean overflowToDisk = false;
java.lang.String diskStorePath = null;
boolean eternal = false;
long timeToLiveSeconds = 30 * 60;
long timeToIdleSeconds = 20 * 60;
boolean diskPersistent = false;
long diskExpiryThreadIntervalSeconds = 120;
RegisteredEventListeners registeredEventListeners = null; Cache IDCache = new Cache(name, maxElementsInMemory, memoryStoreEvictionPolicy, overflowToDisk, diskStorePath,
eternal, timeToLiveSeconds, timeToIdleSeconds, diskPersistent, diskExpiryThreadIntervalSeconds,
registeredEventListeners);
cacheManager.addCache(IDCache);
Cache cache = cacheManager.getCache(name);
long start = System.currentTimeMillis();
for(int i=0; i<100000; i++) {
Element element1 = new Element(""+i, i);
// cache.put(element1,true);
cache.putQuiet(element1);
}
long end = System.currentTimeMillis();
System.out.println(end-start + "ms");
System.out.println(cache.get("1000").getObjectValue());
System.out.println(cache.get("1000").getObjectValue());
CacheManager.getInstance().removalAll();
CacheManager.getInstance().shutdown();
}
}