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
admdev
  • 448
  • 2
  • 9

1 Answers1

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();
    }
}
Henri
  • 5,551
  • 1
  • 22
  • 29
  • 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 Aug 20 '18 at 08:52
  • Like everything in Spring. You inject it. I will update my answer to show you. – Henri Aug 21 '18 at 13:13