Spring @Cacheable 默认 ttl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44202700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:25:58  来源:igfitidea点击:

Spring @Cacheable default ttl

springspring-mvccachingspring-cache

提问by Anand Sunderraman

I generally use the @Cacheablewith a cache config in my spring-boot app and set specific TTL (time to live) for each cache.

我通常@Cacheable在我的 spring-boot 应用程序中使用缓存配置,并为每个缓存设置特定的 TTL(生存时间)。

I recently inherited a spring boot app that uses @Cacheablewithout explicitly stating a cache manager and ttl. I will be changing it to be explicit.

我最近继承了一个 Spring Boot 应用程序,该应用程序使用时@Cacheable没有明确说明缓存管理器和 ttl。我将把它改成明确的。

But I am not able to find out what are the defaults when there is nothing explicit.

但是当没有明确的内容时,我无法找出默认值是什么。

I did look at the docsbut found nothing there

我确实看过文档,但什么也没找到

采纳答案by John Blum

Springis pretty clear about TTL/TTI (Expiration) and Eviction policies as explained in the core Spring Framework Reference Guidehere. In other words, the "defaults" depend entirely on the underlying data store (a.k.a. caching provider) used with the Spring Bootapp via the Spring Cache Abstraction.

春季为核心解释的是关于TTL / TTI(过期),并驱逐策略很清楚Spring框架参考指南这里。换句话说,“默认值”完全取决于通过Spring Cache AbstractionSpring Boot应用程序一起使用的底层数据存储(又名缓存提供程序)。

While Arpit'ssolution is a nice workaround and a generic, transferable solution across different caching providers (data stores), it also cannot cover more specific expiration/eviction policies, such as the kind of action to perform when an expiration/eviction, say OVERFLOW_TO_DISK, or LOCAL_DESTROY only (such as in a Highly Available (maybe zoned based), distributed scenario), or INVALIDATE, etc.

虽然Arpit 的解决方案是一种很好的解决方法,并且是跨不同缓存提供程序(数据存储)的通用、可转移的解决方案,但它也不能涵盖更具体的到期/驱逐策略,例如到期/驱逐时要执行的操作类型,例如 OVERFLOW_TO_DISK,或仅 LOCAL_DESTROY(例如在高可用(可能基于分区)、分布式场景中)或 INVALIDATE 等。

Usually, depending on the UC, evicting "all" entries is not an acceptable option and is one of the reasons why Springdelegates this responsibility to caching provider as this capability varies highly between 1 provider to another.

通常,根据 UC,驱逐“所有”条目是不可接受的选项,这也是Spring将此责任委托给缓存提供者的原因之一,因为此功能在一个提供者与另一个提供者之间差异很大。

In summary... definitely review the requirements for your UC and pair the appropriate caching provider with the capabilities that match your UC. Springsupports a wide variety of caching providersfrom Redisto Apache Geode/Pivotal GemFireto Hazelcast, etc, each with different/similar capabilities in this regard.

总之……一定要查看您的 UC 的要求,并将适当的缓存提供程序与与您的 UC 相匹配的功能配对。 Spring支持各种缓存提供程序,RedisApache Geode/Pivotal GemFireHazelcast等,每个在这方面都有不同/相似的功能。

回答by Anthony Anyanwu

With spring boot, I was able to achieve success with this:

使用 spring boot,我能够通过以下方式取得成功:

First, you need to add spring-boot-starter-data-redisartifact to your POM file.

首先,您需要将spring-boot-starter-data-redis工件添加到 POM 文件中。

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-redis</artifactId> 
</dependency>

After this, you need to add the required the following configurations in your application.properties files:

在此之后,您需要在 application.properties 文件中添加所需的以下配置:

#------ Redis Properties -------------
spring.cache.type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.cache.redis.time-to-live=600000

The property to set is spring.cache.redis.time-to-live(value is in milliseconds. 10 mins was set in this case). With this, @Cacheableworks with the set default TTL.

要设置的属性是spring.cache.redis.time-to-live(值以毫秒为单位。在本例中设置为 10 分钟)。有了这个,@Cacheable使用设置的默认 TTL。

回答by Arpit Aggarwal

Spring @Cacheabledoes not have any configurable option to set TTLfor the cache though you can build it using @CacheEvictand @Scheduled, as follows:

Spring @Cacheable没有TTL为缓存设置任何可配置选项,尽管您可以使用@CacheEvict@Scheduled构建它,如下所示:

@CacheEvict(allEntries = true, cacheNames = { "cache_1", "cache_2" })
@Scheduled(fixedDelay = 30000)
public void cacheEvict() {
}

You can find detailed solution/explanation here - Setting TTL for @Cacheable – Spring.

您可以在此处找到详细的解决方案/说明 - Setting TTL for @Cacheable – Spring

回答by adamine

Actually, there is a better way than using @schedule, by extending the cacheManager which defines the ttl: Here is an example :

实际上,通过扩展定义 ttl 的 cacheManager,有比使用 @schedule 更好的方法:这是一个示例:

@Configuration
public class CacheConfig extends CachingConfigurerSupport {

@Value( "${redis.hostname}" )
private String redisHostName;

@Value( "${redis.port}" )
private int redisPort;

@Value("#{${redis.ttl}}")
private int DEFAULT_TTL;


@Bean
public JedisConnectionFactory redisConnectionFactory() {
    JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
    redisConnectionFactory.setHostName(redisHostName);
    redisConnectionFactory.setPort(redisPort);
    return redisConnectionFactory;
}

@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(cf);
    return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
    cacheManager.setDefaultExpiration(DEFAULT_TTL);
    return cacheManager;
}
}

pom dependency :

pom依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

回答by KUMARESAN KASILINGAM

The cache will never expire by default. If we need to set expire time we have to use the below properites key. If the value will is the 10000ms, the cache will expire after 1 minute.

默认情况下缓存永远不会过期。如果我们需要设置过期时间,我们必须使用下面的属性键。如果值为 10000 毫秒,则缓存将在 1 分钟后过期。

# Entry expiration. By default, the entries never expire.
spring.cache.redis.time-to-live=10000ms