SpringBoot监听redis过期key

开启过期监听

vim /etc/redis.conf
取消notify-keyspace-events Elg的注释
SpringBoot监听redis过期key_第1张图片

pom.xml

添加:


            org.springframework.boot
            spring-boot-starter-data-redis
 

            org.apache.commons
            commons-pool2
            2.7.0

完整如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.wzq
    redis
    0.0.1-SNAPSHOT
    redis
    Spring redis

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.apache.commons
            commons-pool2
            2.7.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



application.yml

spring:
  redis:
    host: 192.168.0.45
    port: 6379
    password: 123456
    lettuce:
      pool:
        #最大等待连接中的数量,设 0 为没有限制
        max-idle: 8
        #最大连接数据库连接数,设 0 为没有限制
        max-active: 8
        #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
        max-wait: -1ms
        #最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请。
        min-idle: 0

#    jedis:
#      pool:
#        max-active: 8
#        max-wait: -1ms
#        max-idle: 8
#        min-idle: 0

创建Redis消息监听器容器

SpringBoot监听redis过期key_第2张图片

package com.wzq.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @description:
 * @author: Wzq
 * @create: 2019-12-06 16:13
 */
@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

}

监听方法

SpringBoot监听redis过期key_第3张图片

package com.wzq.redis;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: Wzq
 * @create: 2019-12-06 16:11
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
        String expiredKey = message.toString();
        System.out.println(expiredKey);
    }
}

个人微信公众,经常更新一些实用的干货:
image.png

你可能感兴趣的:(redis,SpringBoot)