Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache invalidation in Ehcache

Tags:

ehcache

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?

like image 379
user729344 Avatar asked Apr 29 '11 09:04

user729344


Video Answer


2 Answers

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.

like image 171
lukin Avatar answered Nov 08 '22 17:11

lukin


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();
}
like image 21
Ivan Davidov Avatar answered Nov 08 '22 17:11

Ivan Davidov