deepika azad
deepika azad

Reputation: 191

Micronaut caffeine cache expiry is not working

micronaut:
  application:
    name: isac-card
  cache:
    redis:
      enabled: false
    caffeine:
      enabled: true
      caches:
        keyConfig:
          maximum-size: 1000
          expire-after-write: 2s

expire-after-write in micronaut is not getting expired after the set time.

  @Test
    void testCacheExpiry() {
        System.out.println("First Call: " + cacheService.getValue()); // Fetches from AWS
        System.out.println("Second Call: " + cacheService.getValue()); // Should be cached
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("After 3s: " + cacheService.getValue());
        cacheService.invalidateValue();
    }
     

here is my service code

@Cacheable(value = "keyConfig")
    public String getValue() {
        // fetch from db 
        return value
    }

    @CacheInvalidate("keyConfig")
    public void invalidateValue() {
        System.out.println("Invalidating cache...");
    }

But when i tried invalidateValue that is working. It invalidate the cache but expiry is not working after 3 sec also value is fetched from cache. Here is the log,

Value found in cache [keyConfig] for invocation: String getValue()

this is after calling invalidateValue

Value not found in cache for invocation: String getValue()

Upvotes: 1

Views: 35

Answers (0)

Related Questions