java – Ehcache丢失对象

前端之家收集整理的这篇文章主要介绍了java – Ehcache丢失对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用Ehcache来缓存在我的应用程序中加载非常昂贵的对象.在启动时,我会缓存所有需要的对象,并且每天通过一个单独的进程刷新一次.

但是,似乎在我将对象添加到缓存后不久,它们就消失了.没有任何东西被报告为被驱逐,但由于某种原因,对象不会在缓存中持续存在.当我运行ObjectLoader.loadCache()时,这是我收到的输出

@H_301_8@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

@H_301_8@@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;
    }
}

ObjectLoader.java

@H_301_8@@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

ehcache.xml中

@H_301_8@

applicationContext.xml中

@H_301_8@factorybean" />
最佳答案
由于diskPersistent设置为true,因此Ehcache在将对象添加到缓存后尝试将对象写入磁盘.但是,主缓存对象中的一个嵌套对象没有实现Serializable,因此它在磁盘写入时抛出异常,然后从缓存中逐出该对象.这种驱逐没有出现在缓存统计中,因为它没有被memoryStoreEvictionPolicy驱逐.

这应该是一个非常容易找到的错误,但该项目不包括slf4j-log4j12库,因此异常被吞下而不是写入日志.一旦正确处理了日志记录,就会立即明白问题是什么.

哦,好吧,生活和学习我猜…

原文链接:https://www.f2er.com/spring/432738.html

猜你在找的Spring相关文章