Redis的Key过期机制

 

这个东西之前配置过,今天本来记录一下,加深印象,结果忙了一下午,问题出在redis的配置中,即将崩溃,这里提个醒,redis的配置文件中,对于每一行配置前面别有空格,正确做法一直顶到头,这个真是恶心的配置,一点都不智能,因为开启key过期要修改配置文件,所以在修改的时候别忘了这个坑。

我使用的windows版本的redis为Redis-x64-3.2.100,使用的是.msi版本的,使用.zip有点问题,也有可能你的没有问题。

redis下载链接:https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100

1、依赖 

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

 <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-pool2</artifactId>
       <version>2.6.2</version>
  </dependency>

2、文件配置

spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.jedis.pool.max-active=10
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=2000
spring.redis.timeout=1000
spring.redis.lettuce.pool.MaxTotal=10
spring.redis.lettuce.pool.minIdle=1
spring.redis.lettuce.pool.maxWaitMillis=50000
spring.redis.lettuce.pool.maxIdle=5
spring.redis.lettuce.pool.testOnBorrow=true
spring.redis.lettuce.pool.testOnReturn=true
spring.redis.lettuce.pool.testWhileIdle=true

3、代码配置

@Configuration
public class RedisConfig {


    @Bean
    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
    public GenericObjectPoolConfig redisPool() {
        return new GenericObjectPoolConfig();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public RedisStandaloneConfiguration redisConfig() {
        return new RedisStandaloneConfiguration();
    }

    @Bean
    @Primary
    public LettuceConnectionFactory connectionFactory(GenericObjectPoolConfig redisPool, RedisStandaloneConfiguration redisConfig) {
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).commandTimeout(Duration.ofMillis(redisPool.getMaxWaitMillis())).build();
        return new LettuceConnectionFactory(redisConfig, clientConfiguration);
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(LettuceConnectionFactory connectionFactory, MessageListenerAdapter redisMsgListener) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.addMessageListener(redisMsgListener, new PatternTopic("__keyevent@0__:*"));
        return listenerContainer;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory connectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        return stringRedisTemplate;

    }
}

4、配置监听器,后续业务处理

@Component
public class RedisMsgListener extends MessageListenerAdapter {

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String topic = new String(pattern);
        RedisSerializer<String> string = RedisSerializer.string();
        String deserialize = string.deserialize(message.getBody());
        System.out.println("topic名称:" + message.getChannel());
        System.out.println("Msg:" + deserialize);
    }
}

5、修改redis配置,重启

redis.windows-service.conf

notify-keyspace-events Ex

6、测试 

  
   @Autowired
    private StringRedisTemplate stringRedisTemplate;

   @GetMapping("/push")
    public void push(String lee) {
        stringRedisTemplate.opsForValue().set(lee, String.valueOf(System.nanoTime()), 3L, TimeUnit.SECONDS);
    }

3秒之后,打印如下:

如果能帮到你,麻烦点个赞哦!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术武器库

一句真诚的谢谢,胜过千言万语

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值