SpringBoot监听redis过期key
image.png
pom.xml
添加:
<dependency><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
完整如下:
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.wzq</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis</name>
<description>Spring redis</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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消息监听器容器
image.png
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
publicclass RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
监听方法
image.png
packagecom.wzq.redis;importorg.springframework.data.redis.connection.Message;
importorg.springframework.data.redis.listener.KeyExpirationEventMessageListener;
importorg.springframework.data.redis.listener.RedisMessageListenerContainer;
importorg.springframework.stereotype.Component;
/**
* @description:
* @author: Wzq
* @create: 2019-12-06 16:11
*/
@Component
publicclassRedisKeyExpirationListenerextendsKeyExpirationEventMessageListener{
publicRedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer){
super(listenerContainer);
}
@Override
publicvoidonMessage(Message message,byte[] pattern){
// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
String expiredKey = message.toString();
System.out.println(expiredKey);
}
}
个人微信公众,经常更新一些实用的干货:
以上是 SpringBoot监听redis过期key 的全部内容, 来源链接: utcz.com/z/513264.html