Hibernate& EHCache:maxElementsInMemory如何工作?

时间:2012-09-21 05:11:36

标签: hibernate configuration ehcache

我已经为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>

1 个答案:

答案 0 :(得分:23)

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

每当基础数据发生变化时,这些结果都需要“无效”。这是UpdateTimestampsCache的目的。当Hibernate写入表时,它会在UpdateTimestampsCache中创建条目(此过程仅在启用查询缓存时启用,因为它明确是使这些缓存的查询结果无效的一部分)。回读高速缓存的查询结果时,我们会检查使用查询结果缓存的时间戳,以及用于确定结果是否仍然有效的所有表的时间戳。如果可能的话,最好不要限制这个区域。如果需要,最佳数字是基础域模型中的表数。否则,缓存的查询结果可能会在不必要时开始失效。很难想象你有10000张桌子,所以你可能很好。