micronaut-cache icon indicating copy to clipboard operation
micronaut-cache copied to clipboard

SyncCache<...>.invalidateAll() is not calling/triggering the method annotated with @CacheInvalidate(all = true)

Open jonathanptew opened this issue 4 years ago • 1 comments

Expected Behavior

    @Inject
    SyncCache<ServiceWithCache> cache;

    @BeforeEach
    void each() {
        cache.invalidateAll();
    }

    @Test
    void singleItemIsCached() {
        final var cacheKey = "cacheKey";

        assertThat(serviceWithCache.getCacheSize()).isEqualTo(0);

        final var cachedValue = serviceWithCache.cachedGet(cacheKey);
...
    }

    @Test
    void multipleItemsAreCached() {
        final var items = 5;
        final var cacheKey = "cacheKey";

        assertThat(serviceWithCache.getCacheSize()).isEqualTo(0);
....
    }

In each test I expect that the number of entries in the internal cache object should be 0.

ServiceWithCacheExplictResetTest behaves as expected but doesn't use micronaut's cache invalidation

Actual Behaviour

cache.invalidateAll() doesn't trigger calls to reset()

    private final Map<String, String> cachedItems = new LinkedHashMap<>();
...

    @CacheInvalidate(all = true)
    public void reset() {
        cachedItems.clear();
    }

Steps To Reproduce

  1. Pull https://github.com/jonathanptew/micronaut-cache-example/
  2. Run tests
  3. ServiceWithCacheInvalidateResetTest fails on subsequent tests where the size of the cache is > 0
  4. ServiceWithCacheExplictResetTest behaves as expected

Environment Information

❯ mvn --version Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d) Maven home: /usr/local/Cellar/maven/3.8.1 Java version: 16.0.2, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/16.0.2/libexec/openjdk.jdk/Contents/Home Default locale: en_NZ, platform encoding: UTF-8 OS name: "mac os x", version: "11.5.2", arch: "x86_64", family: "mac"

Example Application

https://github.com/jonathanptew/micronaut-cache-example/

Version

2.5.13

jonathanptew avatar Sep 13 '21 07:09 jonathanptew

Can you please confirm that this is still an issue in the latest version of Micronaut?

ojebari-ma avatar May 25 '25 21:05 ojebari-ma