17

I've configured EHCache with a defaultCache (for elements), a StandardQueryCache (for queries) and UpdateTimestampsCache (for what I believe is to keep track of database updates...but I don't really get what it excactly does).

I've set the maxElementsInMemory for each of these caches, but what I don't get is what this number controls for the StandardQueryCache and for the UpdateTimestampsCache. I get that the number of entities that can be cached in the default cache must not exceed 10000, but the query cache does not cache elements. It caches primary keys (as far as I understand).

Does this then mean that the maxElementsInMemory for the StandardQueryCache controls the number of "rows" from results, or does it control the number of primary key pairs for elements it may have?

What about the UpdateTimestampsCache? Does it keep track of the last time an entity was updated, the last time a table was updated...or something else? What number should I use for maxElementsInMemory for this one?

Thanks!

<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>
sbrattla
  • 5,274
  • 3
  • 39
  • 63

1 Answers1

25

For query caching, the results from each query result is one entry in the StandardQueryCache region. So your cache there is currently set up to cache 10000 different query results in the default/unnamed region. Queries set to use a named region (Query#setCacheRegion) write to a different cache region.

These results need to be "invalidated" whenever the underlying data is changed. That is the purpose of UpdateTimestampsCache. When Hibernate writes to tables it makes entries into the UpdateTimestampsCache (this process is only enabled when the query cache is enabled as it is explicitly part of invalidating these cached query results). When reading back cached query results, we check the timestamp cached with the query results against the timestamps of all the tables it uses to determine if the results are still valid. Its best really to not limit this region if possible. If you need to, the best number is the number of tables in your underlying domain model. Otherwise, cached query results might start getting invalidated when not necesary. Hard to imagine you have 10000 tables though, so you are probably fine there.

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46