【问题标题】:Spring ehcache - Cache not being retrieved from diskSpring ehcache - 未从磁盘检索缓存
【发布时间】:2018-02-12 17:17:06
【问题描述】:

要求:缓存时,如果为缓存提供的最大堆大小超过,它应该溢出到磁盘并从那里获取它。 我的 ehcache 配置:

cache:
timeToLiveSeconds: 3600
ehcache:
 maxBytesLocalHeap: 1M
 setMaxBytesLocalDisk: 5G
 diskStore: cacheDiskStore

这是 ehcache.xml 配置(被 yml 中提供的值覆盖):

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" name="CM1"
updateCheck="false" maxBytesLocalHeap="1M" maxBytesLocalDisk="1M" maxEntriesLocalDisk="100000">

<!-- This is a default configuration, it is re-configured by the CacheConfiguration 
    Spring Bean, using the properties from the resources/config/*.yml files. -->

<diskStore path="cacheDiskStore" />

<defaultCache eternal="false" overflowToDisk="true" />
<cache name="cache1" >
    <persistence strategy="localTempSwap" /> 
</cache>
<cache name="cache2" >
    <persistence strategy="localTempSwap" /> 
</cache>

我正在缓存大型媒体文件。但是当我尝试从缓存中检索元素时,它总是为空。这就是我从缓存中检索的方式:

Element elt = CacheManager.getInstance()
            .getEhcache("<cacheName>").get(<key>);
    if (elt != null) {
        System.out.println("**** Retrieved from cache *****");
        return elt;
    } else {
        System.out.println("**** NOT FOUND IN CACHE *****");
        return null;
    }

它不是从缓存中挑选的。 如果我增加缓存堆大小,它可以正常工作。有什么帮助吗?

【问题讨论】:

    标签: spring ehcache


    【解决方案1】:

    在 Ehcache 2.x 中,磁盘存储仍然为键消耗堆。因此,您的某些缓存内容完全有可能被驱逐,因为在磁盘上保存的映射键和堆上的映射之间,您最终会导致驱逐。

    另请注意,如果在应用程序退出时缓存管理器完全关闭,开源磁盘存储将仅在重新启动时恢复数据。即必须调用CacheManager.shutdown()

    【讨论】:

    • 嗨。谢谢。那么应该如何避免这种驱逐呢?
    • 正如您在问题中所说的那样,通过增加堆大小为缓存提供更多空间。确切的数量以及最适合您的用例的数量必须通过测试来确定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2015-06-21
    • 2020-07-21
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多