繁体   English   中英

Hibernate 2nd level cache ehcache timeToLive to short

[英]Hibernate 2nd level cache ehcache timeToLive to short

我为 spring 应用程序中的某些实体配置了带有 ehcache 的二级休眠缓存。

缓存应该存活 10 分钟,但缓存的实体似乎没有那么长。

我的实体看起来像这样:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_apples")
public class Apple {
...
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "eh_eaters")
    @ManyToMany(fetch = FetchType.EAGER)
    private Set<AppleEater> eaters = new HashSet<AppleEater>();
....
}

persistence.xml 的一部分:

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_minimal_puts" value="true"/>    
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.cache.provider_configuration_file_resource_path" value="META-INF/ehcache.xml"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"></property>

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" 
updateCheck="true"
monitoring="autodetect" 
dynamicConfig="true" 
maxBytesLocalHeap="500M">

<diskStore path="java.io.tmpdir/ehcache" />

<cache
    name="eh_apples" 
    eternal="false"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LRU" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache
    name="eh_eaters" 
    eternal="false"
    overflowToDisk="false"
    memoryStoreEvictionPolicy="LRU" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache 
    name="org.hibernate.cache.internal.StandardQueryCache"
    eternal="false" 
    timeToLiveSeconds="600">
    <persistence strategy="localTempSwap" />
</cache>

<cache 
    name="org.hibernate.cache.spi.UpdateTimestampsCache"
    eternal="true">
    <persistence strategy="localTempSwap" />
</cache>

</ehcache>

在本地主机上:

第一次从数据库中检索 500 个苹果时,大约需要 1000 毫秒。 日志显示它们被放入内存中。 然后对于以下一些请求,它不再访问数据库而是从内存中读取它们。

不到 10 分钟后,它开始再次访问相同实体的数据库,如下所示:

时间:09:37:44.143 持续时间:903
时间:09:37:53.295 持续时间:92
时间:09:37:58.278 持续时间:67
时间:09:38:57.701 持续时间:61
时间:09:39:25.384 持续时间:55
时间:09:40:10.049 持续时间:1185
时间:09:44:21.507 持续时间:1005
时间:09:44:24.802 持续时间:99

我想知道为什么缓存不能活到 10 分钟。 也许我在阅读 ehcache 文档时错过了一些重要的部分,或者我的设置存在一些问题。

我感谢任何提示或帮助,请不要讨厌 <3

编辑:运行统计

当我运行统计时,我收到以下警告:

2017-03-04 10:45:54,563 [tomcat-http--90] WARN  net.sf.ehcache.config.ConfigurationFactory - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/C:/repo/sts-bundle/pivotal-tc-server-developer-3.2.2.RELEASE/base-instance/wtpwebapps/Apples/WEB-INF/lib/ehcache-2.10.2.2.21.jar!/ehcache-failsafe.xml

我的第一个猜测是你达到了 maxEntriesLocalHeap 或 maxBytesLocalHeap 这会导致驱逐。

您可以通过查看统计数据来确认这一点。

问题是: net.sf.ehcache.config.ConfigurationFactory - No configuration found.

从 ehcache-failsafe.xml 配置 ehcache

解决方案是,将 ehcache 从META-INF移动到src/main/resources

<property name="hibernate.cache.provider_configuration_file_resource_path" 
          value="classpath:ehcache.xml"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM