Spring @Cacheable 默认 TTL

Spring @Cacheable default TTL

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

我最近继承了一个 spring 启动应用程序,它使用 @Cacheable 而没有明确说明缓存管理器和 ttl。我会将其更改为明确的。

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

我确实查看了 docs,但在那里什么也没找到

Spring@Cacheable does not have any configurable option to set TTL for the cache though you can build it using @CacheEvict and @Scheduled,如下:

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

您可以在此处找到详细的 solution/explanation - Setting TTL for @Cacheable – Spring

Spring 非常清楚 TTL/TTI(到期)和驱逐政策,如核心 Spring 中所述框架参考指南 here。换句话说,"defaults" 完全依赖于通过 Spring Boot 应用程序使用的底层数据存储(a.k.a。缓存提供程序)Spring 缓存抽象.

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

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

总而言之...一定要查看您的 UC 的要求,并将适当的缓存提供程序与与您的 UC 相匹配的功能配对。 Spring supports a wide variety of caching providers from Redis to Apache Geode/Pivotal GemFireHazelcast 等,每个在这方面都有 different/similar 能力。

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

@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>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

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

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

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

在此之后,您需要在 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

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

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

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