springboot 监听redis key过期

springboot 监听redis key过期

一、前提准备

监听redis的key过期,必须配置redis(redis:6.2.5)

#  开启过期监听(空串关闭)
#  notify-keyspace-events ""
notify-keyspace-events Ex

二、项目代码

pom.xml

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

application.yml

spring:
  redis:
    host: 192.168.1.17
    password: HSJissmart.1215
    port: 6379
    ssl: false
    #连接超时时间 这里用的是Duration时间类型,这里配置是是48小时,如果你不想你的连接超时,把这个配置给注掉
    #connect-timeout: PT48H
    #读取数据超时时间,这里5秒查不到数据就超时
    timeout: PT5S
    database: 8

RedisConfig.java

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

@Component
public class RedisConfig {
    /**
     * @Description:配置Redis消息监听容器
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}

RedisKeyExpirationListener.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @Description:编写一个类交由spring管理并继承KeyExpirationEventMessageListener,一个项目中可有多个监听器实例
 */
@Component
@Slf4j
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

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

    /**
     * @Description:监听db:8
     */
    @Override
    protected void doRegister(RedisMessageListenerContainer listenerContainer) {
        //database: 8
        listenerContainer.addMessageListener(this, new PatternTopic("__keyevent@8__:expired"));
    }

    /**
     * 过期消息
     *
     * @param message key
     * @param pattern 消息事件
     * @return void
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();
        if (expiredKey.contains("TIME_OUT_KEY")) { // 判断是否是想要监听的过期key
            log.info("redis TIME_OUT_KEY过期:{}", expiredKey);
            //pattern:__keyevent@*__:expired
            log.info("redis pattern:{}", new String(pattern, StandardCharsets.UTF_8));
            // TODO 业务逻辑
        }

    }
}

SpringBootMainApplication.java

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class SpringBootMainApplication implements CommandLineRunner {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

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

	@Override
	public void run(String... args) throws Exception {
	   try {
            stringRedisTemplate.opsForValue().set("TIME_OUT_KEY_123456789","123456789",5, TimeUnit.MINUTES);
            stringRedisTemplate.opsForValue().set("TIME_OUT_KEY_666666666","666666666",15, TimeUnit.MINUTES);
            stringRedisTemplate.opsForValue().set("TIME_OUT_KEY_888888888","888888888",30, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}

转载:HSJ0170

链接:https://blog.csdn.net/HSJ0170/article/details/128671183

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值