我正在尝试从缓存中获取数据,该缓存存储在本地路径的磁盘中。但是当第一个请求从浏览器发送时,它不会进入缓存,而是通过从数据库中提取数据来覆盖现有缓存(尽管缓存中存在相同的数据)。
如果数据已经存在,如何通过不为每个第一个请求创建新数据来从缓存中获取数据
我正在调用像这样的缓存
public class ServiceListener implements ServletContextListener {
private static final Logger log = Logger.getLogger(ServiceListener.class);
public static CacheManager cm;
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
log.info("initialized");
// CacheManager c = CacheManager.newInstance();
cm = CacheManager.getInstance();
}
}
缓存配置
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="/home/teja/projects/Main/cacheExp" />
<cache name="cache1" maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000" eternal="true" diskSpoolBufferSizeMB="20"
timeToIdleSeconds="3000" timeToLiveSeconds="60000"
diskExpiryThreadIntervalSeconds="150" memoryStoreEvictionPolicy="LFU"
diskPersistent="true" transactionalMode="off">
<!-- <persistence strategy="localRestartable" /> -->
</cache></ehcache>
样本用法
String key = width + " " + height;
if (!cache.isKeyInCache(key)) {
log.info("entered database");
List<List> got = support.getDataFromDb(width, height);
imageList = got.get(0);
priceList = got.get(1);
seatList = got.get(2);
widthList = got.get(3);
heightList = got.get(4);
cache.put(new Element(key, got));
} else {
log.info("entered cache");
List got = (List) cache.get(key).getObjectValue();
imageList = (List<String>) got.get(0);
priceList = (List<Float>) got.get(1);
seatList = (List<String>) got.get(2);
widthList = (List<Integer>) got.get(3);
heightList = (List<Integer>) got.get(4);
}
谢谢