【发布时间】:2018-06-02 21:54:51
【问题描述】:
目前我正在为我的 Web 应用程序使用 Spring Web 框架。我已经实现了一个基本的 CacheConfig.java 文件,它读取 ehcache.xml 并创建一个 CacheManager 并用于从缓存中获取元素或将元素添加到缓存中。我的ehcache.cml如下:
<cache name="Cache"
maxEntriesLocalHeap="100"
maxEntriesLocalDisk="0"
eternal="false"
diskSpoolBufferSizeMB="0"
timeToIdleSeconds="43200"
timeToLiveSeconds="43200"
memoryStoreEvictionPolicy="LFU"
>
<persistence strategy="none"/>
如何通过首先从后端数据库 (SOR) 中查找对象来确保刷新缓存,并且只有在成功检索后才会删除旧版本的元素?
编辑:用于刷新策略的 CacheLoader。
public class TestCacheLoader implements CacheLoader {
@Override
public Object load(Object key) throws CacheException {
//return null;
return load(key,null);
}
@Override
public Map loadAll(Collection keys) {
//return null;
return loadAll(keys,null);
}
@Override
public Object load(Object key, Object argument) {
Object obj = null;
try {
obj = ReadFromDatabase(key);
} catch (DatabaseValueNotFoundException e) {
**How can I return the old value here?**
}
return obj;
}
@Override
public Map loadAll(Collection keys, Object argument) {
//return null;
Map newValues = new HashMap<Object,Object>();
for(Object key : keys){
Object value = load(key,null);
if(value != null){
newValues.put(key,value);
}
}
return newValues;
}
@Override
public String getName() {
return null;
}
@Override
public CacheLoader clone(Ehcache ehcache) throws CloneNotSupportedException {
return null;
}
@Override
public void init() {
}
@Override
public void dispose() throws CacheException {
}
@Override
public Status getStatus() {
return null;
}
}
【问题讨论】: