【问题标题】:Hibernate 2nd level cache ehcache timeToLive to shortHibernate 2 级缓存 ehcache timeToLive 缩短
【发布时间】:2017-07-23 07:42:40
【问题描述】:

我为我的 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>

在本地主机上:

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

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

编辑:运行统计数据

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

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

【问题讨论】:

  • persistence.xml 中有 ALL 吗??
  • 不,我读到了这个选项 - 但我不明白 100% 有什么好处。
  • 据我了解 ALL 只会使我所有的实体可缓存。但我不是已经缓存了实体吗?只要我想要它们,它们就不会被缓存:/
  • 是的..您使用的是哪个服务器?
  • Windows 本地:pivot tc server v3.2 或 Tomcat v8.0

标签: java spring hibernate ehcache


【解决方案1】:

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

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

【讨论】:

  • 我用统计数据进行了测试,并将结果添加到问题中。尺寸非常小,永远不会达到给定的限制 - 所以它们不会被驱逐。它们会在 2 分钟后过期。
【解决方案2】:

问题是: 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"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 2013-11-23
    • 2013-11-07
    • 2016-06-05
    相关资源
    最近更新 更多