1

I have an entity A and it is defined as @Cacheable. And I have query cache loading the entity A under specified cache region say "regionA". This is accomplished by setHint and enabled cache.

As for the settings in wildfly, The regionA is configured with no eviction and expiration as 1 day where as the entity cache has default eviction and expiration let say as below.

        <local-cache name="entity">
            <transaction mode="NON_XA"/>
            <eviction strategy="LRU" max-entries="10000"/>
            <expiration max-idle="1000000"/>
        </local-cache>
        <local-cache name="local-query">
            <eviction strategy="LRU" max-entries="10000"/>
            <expiration max-idle="100000"/>
        </local-cache>
        <local-cache name="regionA" statistics-enabled="true">
            <eviction strategy="NONE" max-entries="-1"/>
            <expiration lifespan="86400000" max-idle="14400000"/>
        </local-cache>

Now, if I execute for the 1st time, there are no query cache results, hence SQL is run to fetch the entity. And later the 2nd run, 3rds are seeming to be taken from cache regionA and hence ok. But after 18hrs, I tried to run the same query and it looks like the query is run again.

But I suppose if the query cache is configured as "1 day" expiration, why does it run SQL again ? Is it because the entity cache is expired by that time ? So the entity cache does not take cache region specific settings ? How to distinguish these entities stored in the specific cache region follow the region specific settings.

Thanks.

ulab
  • 1,079
  • 3
  • 15
  • 45

1 Answers1

3

You have max-idle="14400000" which means that if the query was not executed in the past 4 hours, the cached result expires.

Radim Vansa
  • 5,686
  • 2
  • 25
  • 40
  • Thanks for the finding. So is there any way to specify the entity to use regionA cache configuration and store them in specified cache region ? Because by default each entity creates under its own region. – ulab May 05 '17 at 11:32
  • You need to use Hibernate's `org.hibernate.annotation.Cache` which allows to define the region. Or set this externally through the `orm.xml`. – Radim Vansa May 05 '17 at 11:57
  • Thanks I got it. So using hibernate.cfg, either I can configure ALL entities to use the specified region or EACH entity to use the specified region. But it is not possible to specify only the entities that are referenced in query that has cache region as regionA. right? – ulab May 05 '17 at 12:08
  • 1
    IIUC your q - no, each entity will always end up in the cache/region according to its type. There's no way to configure those loaded using a query would end up in the region used for the query itself (or any other hinted region). – Radim Vansa May 10 '17 at 18:58
  • Thanks that explains. – ulab May 11 '17 at 10:47