如何在 Micronaut 的应用程序 YAML 中定义 Duration 对象

ATa*_*r95 3 caching yaml caffeine micronaut caffeine-cache

我试图在 micronaut 中定义咖啡因缓存,并通过应用程序 yaml 设置过期时间。

文档表明

micronaut.caches.discovery-client.expire-after-write应该被定义为一个Duration对象,但不清楚如何在 YAML 中这样做。

---
micronaut:
  application:
    name: example-service
  caches:
    example-cache:
      expire-after-write: 86400
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

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
Run Code Online (Sandbox Code Playgroud)

大概是因为该值被视为一个Integer而不是一个Duration

ATa*_*r95 5

看来你可以像这样简单地做到这一点:

---
micronaut:
  application:
    name: example-service
  caches:
    example-cache:
      expire-after-write: 24h
Run Code Online (Sandbox Code Playgroud)