I want to remove particular cache keys. I am using java spring.
I have different keys under same cache name. I have to remove some particular keys, not the whole cache.
The cache code is as below,
@CacheEvict(value="MyCache", key="CACHE_LIST_COLUMNS + #accountId + '_' + #formType")
public void addListColumn(){.. my code..}
@CacheEvict(value="MyCache", key="CACHE_SUMMARY_FIELDS + #accountId + '_' + #formType")
public void addSummaryColumn(){.. my code..}
Now as you can see under name 'MyCache' I have to different caches with different keys. Can someone guide me how we can get the particular key from a particular cache and remove that cache.
I want to remove this cache list:
value="MyCache", key="CACHE_SUMMARY_FIELDS + #accountId + '_' + #formType"
This is what I am trying
 StringBuffer cacheNames = new StringBuffer();
 for (String cacheName : getCacheManager().getCacheNames()) { 
 Cache cache = getCacheManager().getCache(cacheName);
 cache.clear();
 }
What this code does is gets cache of name 'MyCache' and clears this whole MyCache. But I don't want to clear all the cache entries. For example the keys are,
CacheEvict(value="MyCache", key="CACHE_LIST_COLUMNS + 10000 + '_' + 3")
CacheEvict(value="MyCache", key="CACHE_SUMMARY_FIELDS + 10000 + '_' + 4")
So the keys are Key1 = CACHE_LIST_COLUMNS10000_3
 Key2 = CACHE_SUMMARY_FIELDS10000_4
Now I want to remove only key CACHE_LIST_COLUMNS10000_3. Hence how can I get the cache 'MyCache' and Key CACHE_LIST_COLUMNS10000_3, and remove only data related to this key(CACHE_LIST_COLUMNS10000_3).
I have to explicitly remove cache through java code. Not through annotations. What my function will be is get cache keys as input and delete only those particular keys.
If still you don't understand my question please let me know.
- 
 It's not completely clear what you are asking, but you can refer to arguments in method signature as part of your eviction using spring expression language. See stackoverflow.com/a/10938115/3166303leeor– leeor2016年01月04日 13:27:13 +00:00Commented Jan 4, 2016 at 13:27
4 Answers 4
Seems the only thing you are missing is:
cacheManager.getCache("MyCache").evict(ObjectToEvict);
It would be possible to get a reference to this object (with the key you want to evict), and use the method above.
1 Comment
The below code is working for me,
for (String cacheName : getCacheManager().getCacheNames()) {
logger.info("Clearing cache: " + cacheName);
Cache cache = getCacheManager().getCache(cacheName);
Object obj = cache.getNativeCache();
if (obj instanceof net.sf.ehcache.Ehcache) {
Ehcache ehCa = (Ehcache)obj;
List<Object> keys = ehCa.getKeys();
for (Object key : keys) {
 String keyString = (String)key;
 if (keyString.equalsIgnoreCase("CACHE_LIST_COLUMNS_10000_2")) 
 {
 cache.evict(key);
 }
 }
}
}
Comments
With that code, you don't have any cache. The @CacheEvict annotation is used only to remove items from the specified cache, not to put them.
Assuming that you want to stick with Spring annotations:
- cache-put: put @Cacheableannotation on methods you want to cache.
- cache-evict: put @CacheEvictannotation on methods that should trigger a removal from a cache
Note that the key generation algorithm must be consistent between @Cacheable and @CacheEvict, otherwise you may end up with a stale cache.
Have a read of the Cache Abstraction section of Spring official docs.
Comments
you can use SPEL like this.
@Cacheable(value = "mainTables", key = "#word.concat(':').concat(#image)")
SPEL can see