2

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.

Saif
3,6205 gold badges34 silver badges59 bronze badges
asked Jan 4, 2016 at 13:05
1
  • 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/3166303 Commented Jan 4, 2016 at 13:27

4 Answers 4

3

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.

answered Jan 4, 2016 at 14:32
Sign up to request clarification or add additional context in comments.

1 Comment

Good to hear! It would be great if you could 'close' the question by choosing the answer that worked for you, or creating your own answer and providing your solution there. Cheers!
1

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);
 }
 }
}
}
answered Jan 5, 2016 at 9:37

Comments

0

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:

  1. cache-put: put @Cacheable annotation on methods you want to cache.
  2. cache-evict: put @CacheEvict annotation 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.

answered Jan 4, 2016 at 13:47

Comments

0

you can use SPEL like this.

@Cacheable(value = "mainTables", key = "#word.concat(':').concat(#image)")

SPEL can see

answered Jan 9, 2016 at 3:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.