简体   繁体   English

如何在没有Guava CacheBuilder的情况下为简单@Cacheable设置TTL

[英]How to set a TTL for Simple @Cacheable without Guava CacheBuilder

I wanted to use the @Cacheable annotation for Spring Boot 1.5, without any external cache provider. 我想对Spring Boot 1.5使用@Cacheable批注,而无需任何外部缓存提供程序。 How do I set the TTL for the simple provider case? 如何为简单提供程序设置TTL?

According to this question and other resources online, I can use Guava's CacheBuilder to set the expiry by providing a CacheConfiguration . 根据这个问题和其他在线资源,我可以使用Guava的CacheBuilder通过提供CacheConfiguration来设置过期时间。

However, it seems Guava Cache is deprecated by Spring . 但是,似乎Spring弃用了 Guava Cache。 So without Guava, how does one set the TTL for a simple Spring cache? 因此,如果没有番石榴,如何为简单的Spring缓存设置TTL?

you can try 你可以试试

Ehcache 高速缓存

add dependency in your pom.xml 在您的pom.xml中添加依赖项

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.6.2</version>
</dependency>  

implement cache 实现缓存

@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. 参考的更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM