gpt4 book ai didi

java - 如何在没有 Guava CacheBuilder 的情况下为 Simple @Cacheable 设置 TTL

转载 作者:行者123 更新时间:2023-12-02 02:57:26 24 4
gpt4 key购买 nike

我想在 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;
}
}

more details for ref.

关于java - 如何在没有 Guava CacheBuilder 的情况下为 Simple @Cacheable 设置 TTL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com