【问题标题】:Ehcache 2.4.2 does not write all the values in the element to a fileEhcache 2.4.2 不会将元素中的所有值写入文件
【发布时间】:2011-10-26 19:43:29
【问题描述】:

我正在尝试用 ehcache 做一个简单的测试 - 将一个元素放入缓存中,刷新并关闭缓存。然后用spring重新加载所有bean(也初始化cachemanager)。执行 cache.get 并检索以前写入的值。

EhCache 元素的值是一个称为 DOM 的可序列化类,它包含一个字段 ConcurrentHasMap

我创建了 3 个 DOM 实例:d1、d2、d3 d1(有一个包含 3 个值的地图:t1、t2、t3) d2(有一张有 2 个值的地图:x1,x2) d3(有一个有2个值的地图:s1,s2)

我打电话:

cachemanager and cache are created with spring
cache.put(new Element(1,d1))
cache.put(new Element(2,d2))
cache.put(new Element(3,d3))
cache.flush();
cacheManager.shutdown();
cache = null
cacheManager = null

我调用加载 spring 应用程序上下文(创建 cacheManager 和缓存)

我打电话:

actualD1 = cache.get(1)
actualD2 = cache.get(2)
actualD3 = cache.get(3)

我将 DOM 对象接收到 actualD1、actualD2 和 actualD3 变量中 但问题是现在他们每个人都只有一个值

actualD1(有一个值为 1 的地图:t1) actualD2(有一个值为 1 的地图:x1) actualD3(有一个值为 1 的地图:s1)

可能是什么问题!???

这是我的 ehcache.xml 文件:

<defaultCache
       maxElementsInMemory="1000000"
       eternal="false"
       diskSpoolBufferSizeMB="100"
       overflowToDisk="true"
       clearOnFlush="false"
       copyOnRead="false"
       copyOnWrite="false"
       diskExpiryThreadIntervalSeconds="300"
       diskPersistent="true">
</defaultCache>

这里是我创建缓存管理器的方法(这个方法在 spring 启动时调用)

protected def checkAndCreateCacheManagerIfNeeded() =
{
    if (cacheManager == null)
    {
        synchronized
        {
            if (cacheManager == null)
            {
                cacheManager = CacheManager.create(ehCacheConfigFile);
            }
        }
    };
};

以下代码创建缓存:

protected def getOrCreateCache(cacheName : String) =
{
    checkAndCreateCacheManagerIfNeeded();
    var cache = cacheManager.getEhcache(cacheName);
    if (cache == null)
    {
        cacheManager.synchronized
        {
            cache = cacheManager.getEhcache(cacheName);
            if (cache == null)
            {
                cache = cacheManager.addCacheIfAbsent(cacheName);
            }
        }
    };
    cache;
};

【问题讨论】:

    标签: element store ehcache


    【解决方案1】:

    问题是将t1t2t3 添加到d1 而不将更新的d1 放入缓存。每次将值添加到映射后。必须添加调用:

    cache.put(d1Element)
    

    【讨论】:

      【解决方案2】:

      虽然这不一定是真的,因为它取决于您的设置,但确实认为使用 Ehcache 这样做是最佳实践。在这个特定的设置中,您的元素被序列化到磁盘(但不一定在放置时)。因此,一旦序列化,对对象图的任何后续更改都不会反映在 DiskStore 中。

      这适用于仅 onHeap 的存储,但仍建议您放回原处,以便您将来更改缓存配置而无需更改代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 2018-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多