【问题标题】:How can we configure TTL for RedisHash using SpringBoot?我们如何使用 Spring Boot 为 Redis Hash 配置 TTL?
【发布时间】:2020-10-01 12:04:45
【问题描述】:

我正在尝试在 RedisHash 上配置 TTL。我想为所有密钥设置相同的到期时间。

第一个:我尝试在实体类上添加注释@RedisHash(value="MyHash",timeToLive=60)。

第二个:使用@TimetoLive 和@RedisHash(value="MyHash",timeToLive=60) 添加一个新字段作为过期字段

@RedisHash(value = "MyHash", timeToLive = 60L)
public class MyHash {
.../attributes with few indexes
     @TimeToLive
    private Long expiration;
}

3rd:添加了带有 KeyspaceConfiguration 的 @EnableRedisRepositories

@EnableRedisRepositories(basePackageClasses = MyHash.class, keyspaceConfiguration = MyKeyspaceConfiguration.class)
public class RedisConfig {
//LettuceConnectionFactory
//RedisTemplate
}

public class MyKeyspaceConfiguration extends KeyspaceConfiguration {
    @Override
    public boolean hasSettingsFor(Class<?> type) {
        return true;
    }

    @Override
    public KeyspaceSettings getKeyspaceSettings(Class<?> type) {

        KeyspaceSettings keyspaceSettings = new KeyspaceSettings(MyHash.class, "MyHashlog");
        keyspaceSettings.setTimeToLive(60L);

        return keyspaceSettings;
    }
}

我的仓库:

public interface MyHashRepository extends CrudRepository<MyHash, Long> {

    List<MyHash> findByApplicationId(String applicationId) ;
}

以上所有方法都没有设置任何到期时间。当我签入 Redis 时,它显示 -1。

TTL MyHash:applicationId:e1hd9-w6q0s-5jd3e-wi2h4
(integer) -1

【问题讨论】:

    标签: spring-data-redis


    【解决方案1】:

    我能够通过两种方式解决这个问题:

    解决方案 1: 将@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600) 添加到我的配置类中

    @Configuration
    @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3000)
    open class RedisConfig{}
    
    

    解决方案 2: 扩展RedisHttpSessionConfiguration 类并调用setMaxInactiveIntervalInSeconds

    @Configuration
    open class RedisConfig: RedisHttpSessionConfiguration() {
    
        @PostConstruct
        open fun afterPropertiesSet() {
            val ttlInSeconds = 3000
            this.setMaxInactiveIntervalInSeconds(ttlInSeconds)
        }
    

    我选择了后者,以便最终将 ttl 添加为配置值。

    【讨论】:

    • 这仅适用于spring-session,不适用于spring-data-redis
    【解决方案2】:

    找到解决方案: 我们需要添加 enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP 属性如下,

    方案一:使用@RedisHash设置TTL

    //Add annotation on config or Spring boot main class
    @EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
    @SpringBootApplication
    public class RedisLogServiceApplication implements WebMvcConfigurer {
    
        public static void main(String[] args) {
            SpringApplication.run(RedisLogServiceApplication.class, args);
        }
    }
    
    @RedisHash(value = "MyHash", timeToLive = 60L)
    public class MyHash {
    .../attributes with few indexes
        @Id
        private Long id;
        @Indexed
        private String applicationId;
    }
    

    方案二:使用 KeySapceConfiguration 设置 TTL

    //Add annotation on config or Spring boot main class
    @EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP, keyspaceConfiguration = MyKeyspaceConfiguration.class)
    @SpringBootApplication
    public class RedisLogServiceApplication implements WebMvcConfigurer {
    
        public static void main(String[] args) {
            SpringApplication.run(RedisLogServiceApplication.class, args);
        }
    }
    
    //MyKeyspaceConfiguration.class to set TTL
    public class MyKeyspaceConfiguration extends KeyspaceConfiguration {
        @Override
        public boolean hasSettingsFor(Class<?> type) {
            return true;
        }
    
        @Override
        public KeyspaceSettings getKeyspaceSettings(Class<?> type) {
    
            KeyspaceSettings keyspaceSettings = new KeyspaceSettings(MyHash.class, "MyHashlog");
            keyspaceSettings.setTimeToLive(60L);
    
            return keyspaceSettings;
        }
    }
    
    @RedisHash(value = "MyHash")
    public class MyHash {
    .../attributes with few indexes
        @Id
        private Long id;
        @Indexed
        private String applicationId;
    }
    

    存储库没有变化:

    public interface MyHashRepository extends CrudRepository<MyHash, Long> {
    
        List<MyHash> findByApplicationId(String applicationId) ;
    }
    

    Spring 还将基于在您的实体类中注释为 @Indexed 的属性创建多个键。但是,TTL 仅适用于主键,即@Id。例如, 当我在 redis-cli 中运行 keys 命令时

    >keys MyHash*
    1) MyHash:id:e1hd9-w6q0s-5jd3e-wi2h4
    2) MyHash:applicationId:e1hd9-w6q0s-5jd3e-wi2h4
    
    >TTL MyHash:id:e1hd9-w6q0s-5jd3e-wi2h4
    (integer) 59
    >TTL MyHash:applicationId:e1hd9-w6q0s-5jd3e-wi2h4
    (integer) -1
    
    //After Expiry:
    >keys MyHash*
    (no keys)
    >TTL MyHash:id:e1hd9-w6q0s-5jd3e-wi2h4
    (integer) -2
    

    【讨论】:

      猜你喜欢
      • 2019-10-18
      • 2019-07-25
      • 2021-12-28
      • 2021-07-03
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 1970-01-01
      相关资源
      最近更新 更多