1.1、首先要在redis的配置文件.conf中添加一行代码来开启对失效key的监听
notify-keyspace-events Ex
1.2、导入pom中相关SpringDataRedis坐标
<!--SpringDataRedis的相关坐标-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.4.RELEASE</version>
</dependency>
2、创建相关配置文件
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--整合springDataRedis核心的API: redisTemplate,用来操作redis服务器-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--设置key的序列化方式-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<!--设置value的序列化方式-->
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
</property>
<!--注入连接工厂-->
<property name="connectionFactory" ref="connectionFactory">
</property>
</bean>
<!--创建connectionFactory交给spring容器管理-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1"></property>
<property name="port" value="6379"></property>
<property name="database" value="0"></property>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--创建连接池的基本配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100"></property>
</bean>
<bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter" >
<constructor-arg>
<!--注入自定义的监听器-->
<bean class="cn.fy.listener.RedisMessageListener"></bean>
</constructor-arg>
</bean>
<!--配置监听器容器-->
<bean class="org.springframework.data.redis.listener.RedisMessageListenerContainer" id="redisContainer">
<!--由于需要和redis进行交互,注入连接工厂
1.订阅的消息主题
2.接收到消息之后处理的监听器
-->
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListeners">
<map>
<entry key-ref="messageListener">
<!--topic的集合:订阅的主题,订阅了一个主题消息:主题的名称(itcast)
接收到一条消息之后,交给自定义的消息监听器来进行处理
-->
<list>
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="__keyevent@0__:expired"></constructor-arg>
</bean>
</list>
</entry>
</map>
</property>
</bean>
</beans>
3、配置消息监听器
package cn.fy.listener;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
/**
* 配置redis消息的监听器:
* 来获取redis中的消息并进行处理
* @Author Fy
* @Time 2020年7月10日 11:16:41
* @QQ 1057072154
*/
public class RedisMessageListener implements MessageListener {
/**
* 处理消息
* @param message 完整的消息 (频道的信息以及消息的具体内容)
* @param bytes 获取的频道信息
*/
@Override
public void onMessage(Message message, byte[] bytes) {
System.out.println("从channel为"+new String(message.getChannel())+"获取了一条消息,消息内容为"+new String(message.getBody()));
}
}
4、设置过期时间(四个参数分别是要设置到redis的key和value,还要设置的时长,时间单位)
此处单位是秒,根据需求自己改
redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);
5、在onMessage方法中处理自己的业务逻辑