我想在 Spring Boot 1.5 中使用 @Cacheable 注释,而不需要任何外部缓存提供程序。如何设置 simple 的 TTL提供商案例?
根据this question和其他在线资源,我可以使用 Guava 的 CacheBuilder 通过提供 CacheConfiguration 来设置过期时间。
但是,Guava Cache 似乎是 deprecated by Spring 。那么如果没有 Guava,如何为一个简单的 Spring 缓存设置 TTL?
最佳答案
你可以试试
Ehcache
在 pom.xml 中添加依赖项
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.2</version>
</dependency>
实现缓存
@Service
public class NumberService {
// ...
@Cacheable(
value = "squareCache",
key = "#number",
condition = "#number>10")
public BigDecimal square(Long number) {
BigDecimal square = BigDecimal.valueOf(number)
.multiply(BigDecimal.valueOf(number));
log.info("square of {} is {}", number, square);
return square;
}
}
关于java - 如何在没有 Guava CacheBuilder 的情况下为 Simple @Cacheable 设置 TTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068176/