【问题标题】:Hibernate & EHCache : how does maxElementsInMemory work?Hibernate 和 EHCache:maxElementsInMemory 是如何工作的?
【发布时间】:2012-09-13 12:37:53
【问题描述】:

我已经为 EHCache 配置了 defaultCache(用于元素)、StandardQueryCache(用于查询)和 UpdateTimestampsCache(我认为它是为了跟踪数据库更新......但我并没有真正了解它究竟做了什么)。

我已经为这些缓存中的每一个设置了 maxElementsInMemory,但我没有得到这个数字控制 StandardQueryCache 和 UpdateTimestampsCache 的内容。我知道默认缓存中可以缓存的实体数量不能超过10000,但是查询缓存不缓存元素。它缓存主键(据我所知)。

这是否意味着 StandardQueryCache 的 maxElementsInMemory 控制结果中的“行”数,或者它是否控制它可能具有的元素的主键对数?

UpdateTimestampsCache 怎么样?它是否会跟踪上次更新实体的时间、上次更新表的时间……或其他什么?我应该为这个 maxElementsInMemory 使用什么数字?

谢谢!

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect"     dynamicConfig="true">
  <defaultCache 
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
  </defaultCache>

  <cache
    name="org.hibernate.cache.internal.StandardQueryCache"
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="3600"
    timeToLiveSeconds="3600">
  </cache>

  <cache
    name="org.hibernate.cache.spi.UpdateTimestampsCache"
    maxElementsInMemory="10000"
    eternal="true">
  </cache>

</ehcache>

【问题讨论】:

    标签: hibernate configuration ehcache


    【解决方案1】:

    对于查询缓存,每个查询结果的结果是 StandardQueryCache 区域中的一个条目。因此,您的缓存当前设置为在默认/未命名区域中缓存 10000 个不同的查询结果。设置为使用命名区域 (Query#setCacheRegion) 的查询写入不同的缓存区域。

    每当基础数据发生更改时,这些结果都需要“无效”。这就是 UpdateTimestampsCache 的目的。当 Hibernate 写入表时,它会将条目写入 UpdateTimestampsCache(此过程仅在启用查询缓存时启用,因为它显式地使这些缓存的查询结果无效)。当读回缓存的查询结果时,我们检查与查询结果一起缓存的时间戳与它使用的所有表的时间戳,以确定结果是否仍然有效。如果可能的话,最好不要限制这个区域。如果需要,最好的数字是底层域模型中的表数。否则,缓存的查询结果可能会在不需要时开始失效。很难想象你有 10000 张桌子,所以你可能在那里没问题。

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 2023-04-06
      • 2010-11-23
      • 2013-02-14
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多