How do you define a Duration object in Micronaut's application YAML

1.4k Views Asked by At

I'm trying to define a caffiene cache in micronaut and set the expire time via the application yaml.

The documentation indicates that

micronaut.caches.discovery-client.expire-after-write should be defined as a Duration object but it's unclear how to do so in YAML.

---
micronaut:
  application:
    name: example-service
  caches:
    example-cache:
      expire-after-write: 86400

Gives the following error:

Internal Server Error: Failed to inject value for parameter [expireAfterWrite] of method [setExpireAfterWrite] of class: io.micronaut.cache.caffeine.DefaultCacheConfiguration

Message: Error resolving property value [micronaut.caches.example-cache.expire-after-write]. Property doesn't exist
Path Taken: new $ExampleControllerDefinition$Intercepted(BeanContext beanContext,Qualifier qualifier,[Interceptor[] interceptors]) --> new CacheInterceptor([CacheManager cacheManager],CacheErrorHandler errorHandler,AsyncCacheErrorHandler asyncCacheErrorHandler,ExecutorService ioExecutor,BeanContext beanContext) --> new DefaultCacheManager([List caches],Provider dynamicCacheManager) --> new DefaultSyncCache([CacheConfiguration cacheConfiguration],ApplicationContext applicationContext,ConversionService conversionService) --> DefaultCacheConfiguration.setExpireAfterWrite([Duration expireAfterWrite])
io.micronaut.http.client.exceptions.HttpClientResponseException: Internal Server Error: Failed to inject value for parameter [expireAfterWrite] of method [setExpireAfterWrite] of class: io.micronaut.cache.caffeine.DefaultCacheConfiguration

Presumably as the value is being treated as an Integer not a Duration

1

There are 1 best solutions below

0
On

It appears you can do it as simply as this:

---
micronaut:
  application:
    name: example-service
  caches:
    example-cache:
      expire-after-write: 24h