我正在使用 Ehcache 来缓存在我的应用程序中加载非常昂贵的对象。在启动时,我缓存了所有需要的对象,并且它们每天通过一个单独的进程刷新一次。
但是,似乎在我将对象添加到缓存后不久,它们就消失了。没有报告被驱逐,但由于某种原因,这些对象没有在缓存中持久存在。当我运行 ObjectLoader.loadCache() 这是我收到的输出:
2012-09-01 17:25:37,121 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:25:37,121 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 0
2012-09-01 17:25:37,121 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 501
2012-09-01 17:26:17,271 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:26:17,271 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:26:17,274 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 501
2012-09-01 17:26:17,274 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:26:17,274 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 1
2012-09-01 17:26:17,274 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 518
2012-09-01 17:26:35,894 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:26:35,895 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:26:35,895 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 518
2012-09-01 17:26:35,895 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:26:35,895 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 1
2012-09-01 17:26:35,895 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 516
2012-09-01 17:27:31,997 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:27:31,998 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:27:31,998 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 516
2012-09-01 17:27:31,998 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:27:31,998 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 1
2012-09-01 17:27:31,998 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 515
2012-09-01 17:28:20,343 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:28:20,343 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:28:20,344 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 515
2012-09-01 17:28:20,344 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:28:20,344 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 1
2012-09-01 17:28:20,344 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 525
2012-09-01 17:29:05,616 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:29:05,617 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:29:05,617 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 525
2012-09-01 17:29:05,617 [main] DEBUG ObjectLoader.getObject():65 - ************************************
2012-09-01 17:29:05,617 [main] DEBUG ObjectLoader.getObject():66 - Number of objects in cache: 1
2012-09-01 17:29:05,618 [main] DEBUG ObjectLoader.getObject():72 - Object not in cache | 512
2012-09-01 17:29:38,790 [main] DEBUG ObjectLoader.getObject():80 - Number of objects in cache: 1
2012-09-01 17:29:38,790 [main] DEBUG ObjectLoader.getObject():81 - Number of objects evicted from cache: 0
2012-09-01 17:29:38,790 [main] DEBUG ObjectLoader.getObject():83 - Object key found: 512
是我的 ehcache 配置错误还是我的代码中有错误?任何帮助,将不胜感激。谢谢。
我正在使用 spring 3.1.0、ehcache-spring-annotations 1.1.2 和 ehcache 2.4.2
ObjectCacheFacade.java
@Component()
public class ObjectCacheFacade {
private static final String CACHE_KEY = "myObjectCache";
@Resource
private CacheManager cacheManager;
private Cache cache;
public ObjectCacheFacade() {
cacheManager = CacheManager.getInstance();
this.cache = cacheManager.getCache(CACHE_KEY);
}
public Object getObjectFromCache(String objectId) {
Object result = null;
Element element = cache.get(objectId);
if (element != null && element.getValue() != null) {
result = element.getObjectValue();
}
return result;
}
public void putObjectIntoCache(String objectId, Object object) {
Element element = new Element(objectId, object);
cache.put(element);
}
public int getSize() {
return cache.getSize();
}
public void removeObjectFromCache(String objectId) {
cache.remove(objectId);
}
public void flushCache() {
cache.removeAll();
}
public Cache getCache() {
return cache;
}
}
对象加载器.java
@Service
public class ObjectLoader {
private static final Logger log = Logger.getLogger(ObjectLoader.class);
@Resource
protected ObjectDao objectDao;
@Resource
protected ObjectCacheFacade objectCache;
public void loadCache() {
List<String> objectIds = objectDao.getObjectIds();
for (String objectId : objectIds) {
loadObject(objectId);
}
}
public Object getObject(String objectId) {
log.debug("************************************");
log.debug("Number of objects in cache: " + objectCache.getSize());
Object object = objectCache.getObjectFromCache(objectId);
if (object == null) {
log.debug("Object not in cache | " + objectId);
object = objectDao.getObject(objectId);
if (object != null) {
objectCache.putObjectIntoCache(objectId, object);
log.debug("Number of objects in cache: " + objectCache.getSize());
log.debug("Number of objects evicted from cache: " + objectCache.getCache().getCacheEventNotificationService().getElementsEvictedCounter());
for (Object key : objectCache.getCache().getKeys()) {
log.debug("Object key found: " + key);
}
}
}
return object;
}
}
ehcache.xml
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="300"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="myObjectCache"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="true"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
应用程序上下文.xml
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />