【问题标题】:Setting key specific TTL with @TimeToLive for Redis Spring Caching triggers no invalidation使用 @TimeToLive 为 Redis Spring Caching 设置键特定 TTL 不会触发失效
【发布时间】:2022-08-17 01:12:45
【问题描述】:

我有一个用例,需要在特定时间从缓存中删除单个条目。 TTL 需要在键上而不是在缓存级别上设置

在此spring redis documentation 之后,我尝试实现特定于密钥的 TTL,但它不起作用。没有事件发生,我使用了一个监听器来检查,当缓存 ttl 用完时只有一个事件发生。

缓存的对象有一个用@TimeToLive 注释的字段来自 org.springframework.data.redis.core.TimeToLive 查看文档,这应该会在时间用完后触发过期事件

缓存对象

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BrandResponse {

    @TimeToLive
    private Long ttl;

    @NotBlank
    private String id;
}

使用的依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.6.6</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.6.3</version>
</dependency>

启用键空间事件

@SpringBootApplication
@ServletComponentScan
@EnableAsync
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class KikaRestApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(KikaRestApiApplication.class, args);
    }
}

缓存的默认 TTL 为 5 分钟 .entryTtl(Duration.ofMinutes(5))

缓存设置

@Configuration
@EnableCaching
public class RedisCachingConfiguration {

    private final KikaApiProperties kikaApiProperties;

    @Value(\"${spring.redis.host}\")
    private String host;

    @Value(\"${spring.redis.port}\")
    private Integer port;

    public RedisCachingConfiguration(KikaApiProperties kikaApiProperties) {
        this.kikaApiProperties = kikaApiProperties;
    }

    @Bean
    public RedisCacheConfiguration cacheConfiguration() {
        return RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(5))
            .disableCachingNullValues()
            .serializeValuesWith(
                SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPort(port);
        return new JedisConnectionFactory(configuration);
    }

    @Bean
    public RedisTemplate<String, Idmap> redisTemplate() {
        RedisTemplate<String, Idmap> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }
}

@TimeToLive 不能与 spring-redis 缓存一起使用,我是否缺少一些东西。

  • 也许this 可能会有所帮助。看起来很相似。

标签: java spring caching redis spring-data-redis


【解决方案1】:

根据文档

TimeToLive 将聚合根上的单个数字属性标记为 用于在 Redis 中设置过期时间。注释的属性 取代任何其他超时配置。

RedisHash 将对象标记为要存储在 Redis 中的聚合根 哈希。

默认 ttl 单位是秒。 @TimeToLive(单位 = TimeUnit.SECONDS) 您可以使用其他值,例如 MINUTES。

在 BrantResponse 上使用 @RedisHash。一切顺利

@RedisHash
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BrandResponse {

    @TimeToLive
    private Long ttl;

    @NotBlank
    private String id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多