4

I have a method who's execution should clear two caches in my Spring+JCache+ehcache 3.5 project.

I tried:

@CacheRemoveAll(cacheName = "cache1")
@CacheRemoveAll(cacheName = "cache2")
public void methodToBeCalled(){
}

and

@CacheRemoveAll(cacheName = "cache1", cacheName = "cache2")
public void methodToBeCalled(){
}

In the first I get:

Duplicate annotation of non-repeatable type @CacheRemoveAll

In the second I get:

Duplicate attribute cacheName in annotation @CacheRemoveAll

1 Answer 1

1

You can't. Annotations can't be repeated and attributes can't be repeated.

You would need a @CacheRemoveAlls annotation but the framework hasn't planned that.

Your best solution is simply to call removeAll for your two caches at the beginning of methodToBeCalled.

The code would be like this:

public class MyClass {
    @Autowired
    private CacheManager cacheManager; // this is a Spring CacheManager

    public void methodToBeCalled(){
        cacheManager.getCache("cache1").clear();
        cacheManager.getCache("cache2").clear();
    }
}
2
  • How can I call this method in Java code? I ask because I set up the cache using ehcache.xml and spring xml servlet configuration and I'm only using it via annotations. So how can I reference it in code?
    – admdev
    Commented Aug 20, 2018 at 8:52
  • Like everything in Spring. You inject it. I will update my answer to show you.
    – Henri
    Commented Aug 21, 2018 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.