我正在尝试在我的 spring 项目中配置咖啡因。来自阅读guide,我可以看到有多种方法可以将其配置到您的应用程序中,从创建缓存管理器 bean 到在 application.yml 属性文件中显式写入配置。
到目前为止,我已经使用 application.yml
方法来配置我的咖啡因缓存:
spring:
cache:
type: Caffeine
cache-names: test1
caffeine:
spec: maximumSize=500, expireAfterAccess=30s
我在 Controller 方法中使用了 @Cachable
注释:
@GetMapping
@Cacheable(value = "test1", key = "#accountId")
public DTOStatus getStatus(@PathVariable String accountId) {
if (statusChecker.equals(Check.REQUIRED)) {
deleteAccountFromCache(accountId);
return transformDTO(statusChecker);
} else {
return transformDTO(statusChecker);
}
}
@CacheEvict(value = "test1", key = "#accountId")
public void deleteAccountFromCache(String accountId){
//Method body left blank. The annotation deletes the accountId from the cache.
}
我想知道我是否正确配置了缓存。我目前无法测试它,但只是想确保我已完成启用咖啡因的所有必要步骤
最佳答案
例如,还在配置 java 类中添加 org.springframework.cache.annotation.EnableCaching 注释。
@EnableCaching
class ApplicationConfig {
关于java - 配置spring缓存咖啡因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56932729/