Cache invalidation in Ehcache
Asked Answered
M

5

8

I am using Ehcache in Hibernate.

How can I notify the cache that the database has changed? How can I invalidate the cached data? How can I programmatically achieve this?

More answered 29/4, 2011 at 9:44 Comment(0)
I
11

What do exactly mean? There are 2 ways to make a change in the database: inside the app & outside the app

Inside the app you can trigger easily an invalidation. The second one is a harder to solve. Cache elements are stored with a key and based on that key you can delete it.

CacheManager manager = CacheManager.getInstance();
cache = manager.getCache(cacheName);
cache.remove(key);

or 

cache.removeAll();

Depending how you configured ehcache of course. But you need to know the name of the cache that holds the object.

Iconium answered 27/5, 2011 at 10:47 Comment(0)
G
5

This is how you can invalidate all EhCache items without bothering what the individual cache names are:

CacheManager manager = CacheManager.getInstance();

String[] names = manager.getCacheNames();

for (String name : names)
{
    Cache cache = manager.getCache(name);

    cache.removeAll();
}
Greatnephew answered 1/6, 2011 at 9:24 Comment(2)
While this may have worked well at the time of writing, I had to replace .getCache with .getEhcache, and Cache with Ehcache. Otherwise, .getCache returns null even for caches which exist.Cavy
Didn't work for me as my configurations being hibernate ehcache timeToLiveSeconds not workingFirstborn
C
1

Try:

CacheManager.create().clearAll()

It will clear all caches in CacheManager.

To clear only one particular cache, get it from cache manager and remove all elements:

CacheManager cacheManager = CacheManager.create();
Ehcache cache = cacheManager.getEhcache(cacheConfiguration.getName());
cache.removeAll();    
Cirillo answered 4/11, 2015 at 16:35 Comment(0)
S
1

Set cache expiration so data is refreshed as or more often than the database changes .

Also you can update in cache only elements that change in the database .

Showy answered 13/11, 2018 at 15:44 Comment(3)
Can you explain how to set "cache expiration"?Audi
Cache Expiration is defined by the timeToLiveSeconds field in ehcache.xml ... when the value of timeToLiveSeconds is elapsed , the cached element is considered invalid and will be fetched from the source when requested ... fetching will incur caching thus resetting the timer of timeToLiveSeconds ... For Example : <cache name="thisCache" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="500" timeToLiveSeconds="900" memoryStoreEvictionPolicy="LFU" diskPersistent="false" transactionalMode="off"> </cache>Showy
not applicable to persistent cache entries that never changeGalanti
E
1

In case if you want to remove a specific cache (eg:employeeCache)

@Autowired
CacheManager cacheManager;

place where you want to invalidate cache, use below code

    Cache cache = cacheManager.getCache("employeeCache");
    cache.removeAll();
Exciseman answered 23/7, 2020 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.