我们正在使用 Spring Boot 2 和 Spring Actuator。创建缓存时,如下所示:
@Bean
public CaffeineCache someCache() {
return new CaffeineCache("my-cache",
Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.SECONDS)
.build());
}
它已注册到 Spring Actuator 中,可以通过端点访问和处理:
❯ http GET localhost:8080/actuator/caches
{
"cacheManagers": {
"cacheManager": {
"caches": {
"my-cache": {
"target": "com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalManualCache"
}
}
}
}
}
但是,这在使用注释时是有效的@Cacheable
- 但我想创建一个缓存并将其用作地图。
因此,我可以创建:
@Bean
public com.github.benmanes.caffeine.cache.Cache<String, MyObject> customCache(QueryServiceProperties config) {
return Caffeine.newBuilder()
.maximumSize(10)
.expireAfterAccess(10, TimeUnit.SECONDS)
.build();
}
它可以工作,但不能被Spring Actuator发现。有没有办法注册这种缓存?