Ehcache v2.6.11 timeToLive ignored - cache only clearing with app restart

47 views
Skip to first unread message

Andrew Campbell

unread,
Jul 15, 2021, 12:33:17 PM7/15/21
to ehcache-users
Hi,
I am a relatively amateur user of hibernate and ehcache.
I'm faced with an issue, on a pre-existing application, of a dataset being cached indefinitely until caching is either disabled, or the app has been restarted.

I've configured my ehcache.xml to read like (snippets):

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="300"
overflowToDisk="true"
/>

and cache region

<cache
name="dbItem"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="30"
overflowToDisk="true"
/>

I've decorated the class for the db model like:

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "dbItem")
public class DbItem { .. }

and then queried the database like

List<DbItem> result = entityManager.createQuery(criteria).setHint("org.hibernate.cacheable", true).getResultList();

When I update the database manually, I never see the change reflected in the view of my app until it's been restarted or caching has been switched off entirely.
Hopefully this is enough context, as I say I'm quite new to using this technology.
If you need further information, please let me know.

Many Thanks!

Chris Dennis

unread,
Jul 15, 2021, 12:49:45 PM7/15/21
to ehcach...@googlegroups.com

By “never” I assume you mean after more than 300 seconds you still see the incorrect results? Can you explain how the results are incorrect? Is the list stale (implying it’s the query cache that is incorrect) or are the items in the list themselves stale (hinting at the entity cache). Could you include the full ehcache.xml as well?

 

Chris

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/7309a0f8-25f2-42d3-b9d2-19e3fd9eb313n%40googlegroups.com.

Andrew Campbell

unread,
Jul 16, 2021, 4:17:59 AM7/16/21
to ehcache-users
Hi Chris,
Thank you for your response.

Yes you are correct, I still see the incorrect results after 300 seconds. Specifically, what I'm seeing is a list of 'names' which is a field on the database record 'dbItem.Name' never changing. On first load (after app restart) I might see 'Andy 1' on the list of items through the view of the app. If I update this directly in the database to 'Andy 2' then wait more than 300 seconds, then refresh the page on the browser, I'm still seeing 'Andy 1'. This remains as such until I restart the app where I assume the cache is force dumped.

The entire ehcache.xml is as follows:

<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="300"
timeToLiveSeconds="300"
overflowToDisk="true"
/>
<cache
name="org.hibernate.cache.UpdateTimestampsCache"
maxEntriesLocalHeap="5000"
overflowToDisk="true"
timeToLiveSeconds="300"
timeToIdleSeconds="300"
eternal="false"
/>
<cache
name="org.hibernate.cache.StandardQueryCache"
maxEntriesLocalHeap="500"
overflowToDisk="true"
eternal="false"
timeToLiveSeconds="300"
/>
<cache
name="dbItem"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="30"
timeToLiveSeconds="30"
overflowToDisk="true"
/>

</ehcache>

I know it's not advised to have a TTL or TTI on UpdateTimestampsCache, but for the purposes of debugging I removed the eternal="true" property to see if this made any change to the list of items in the view after 300 seconds, but unfortunately it did not.

Many thanks,
Andy

Chris Dennis

unread,
Aug 2, 2021, 11:36:02 AM8/2/21
to ehcach...@googlegroups.com

Hi Andy,

 

Sorry for the delay in replying… I usually end up answering mailing list emails in batches, but I let this batch build up for a little too long.

 

From memory (and it’s a long time since I did anything with Hibernate caching):

 

I believe having a TTL/TTI on update timestamps cache isn’t wrong as long as entries in the timestamps cache are not expired (or removed) before the query cache entries are.

 

I believe the way the query cache works is to cache the list of primary-ids (or some proxy of/for them) of the entities that the query returns. Hibernate will then look those entities up (potentially returning from the entity cache). In that case therefore it sounds like your problem might be with the entity cache. The first thing I would do would be to ditch the TTI settings on the caches. In a configurationlike this where you want to use TTL to control freshness of the returned data (presumably a timespan tied to some SLA/app requirement) TTI is bad because it can cause load to have the side-effect of extending the lifespan of your cached values. I would drop all the TTI settings and then try the testing again. If that doesn’t bear fruit, my next step would be to turn up debug logging in Hibernate and see if you can determine what caches are getting hit when.

 

Sorry I can’t be of more immediate help,

Reply all
Reply to author
Forward
0 new messages