Hibernate二级缓存ehcache timeToLive为简短

时间:2017-03-03 09:11:39

标签: java spring hibernate ehcache

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

缓存应该存活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>

on localhost:

第一次从DB中检索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文档时错过了一些重要的部分,或者我的设置存在一些问题。

我感谢任何提示或帮助,请不要讨厌&lt; 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

2 个答案:

答案 0 :(得分:1)

我的第一个猜测是你达到maxEntriesLocalHeap或maxBytesLocalHeap而不是导致驱逐。

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

答案 1 :(得分:0)

问题是: net.sf.ehcache.config.ConfigurationFactory - 未找到配置。从ehcache-failsafe.xml配置ehcache

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

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