Ehcache中的缓存失效 [英] Cache invalidation in Ehcache
问题描述
我在Hibernate中使用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?
推荐答案
究竟是什么意思?有两种方法可以更改数据库:在应用程序内部在应用程序外部
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();
当然取决于配置ehcache的方式。但是您需要知道保存该对象的缓存的名称。
Depending how you configured ehcache of course. But you need to know the name of the cache that holds the object.
这篇关于Ehcache中的缓存失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!