Spring+Redis 订阅键过期通知

项目需求:redis缓存的数据生存时间过期,提醒用户去更新缓存

 

♦♦ Step one:开启Reids通知功能的配置

方法一:修改redis/redis.conf配置文件,添加如下设置   (永久有效)

notify-keyspace-events Ex

方法二:连接redis客户端,执行如下语句  (重启redis后失效)

config set notify-keyspace-events Ex

tip : Redis Config Set 命令可以动态地调整 Redis 服务器的配置(configuration),执行后无需重启,直接生效。

 

 ♦♦ Step two: 添加maven依赖 (注意版本问题)

<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>

 

♦♦ Step three:Redis配置

一:redis.properties

#ip地址
redis.host = 114.215.83.3
#redis端口
redis.port = 6380
#auth 验证密码
redis.passwd = 123
#最大能够保持idle状态的对象数  
redis.maxIdle=300  
#最大分配的对象数    注意:低版本的jedis该属性名称是 maxActive
redis.maxTotal=600 
#当池内没有返回对象时,最大等待时间  注意:低版本的jedis该属性名称是 maxWait
redis.maxWaitMillis=1000  
#当调用borrow Object方法时,是否进行有效性检查  
redis.testOnBorrow=true 
#超时时间
redis.timeout=100000

二:spring整合redis的配置文件   applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


	<!-- redis配置文件 -->
	<context:property-placeholder location="classpath:/redis.properties"
		ignore-unresolvable="true" />

	<!-- 配置连接池参数 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<!-- 配置连接工厂 -->
	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.host}" />
		<property name="port" value="${redis.port}" />
		<!-- 如果redis没有开启验证,password不需要配置 -->
		<property name="password" value="${redis.passwd}"></property>
		<property name="timeout" value="${redis.timeout}"></property>
		<property name="poolConfig" ref="poolConfig" />
	</bean>

	<!-- 配置redis消息订阅 -->
	<bean id="messageListener"
		class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
		<constructor-arg>
			<!-- 自定义消息处理类 -->
			<bean class="com.mote.redis.listener.KeyExpiredNotice" />
		</constructor-arg>
	</bean>
	<bean id="redisContainer"
		class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="messageListeners">
			<map>
				<entry key-ref="messageListener">
					<list>
						<!-- 通配符:匹配消息通知类型 -->
						<bean class="org.springframework.data.redis.listener.PatternTopic">
							<constructor-arg value="__key*__:expired" />
						</bean>
					</list>
				</entry>
			</map>
		</property>
	</bean>

</beans>

 ♦♦ Step four:自定义消息处理类

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

@Component
public class KeyExpiredNotice extends MessageListenerAdapter {

	/**
	 * msg : 通知消息,内含 key 
	 * topic : 管道类型
	 */
	@Override
	public void onMessage(Message msg, byte[] topic) {
		String key = new String(msg.getBody());
		// TODO 根据业务书写处理逻辑
		System.out.println(key);
		System.out.println(new String(topic));
	}
}

♦♦ Step five:测试

一:启动项目,断点调试

二:连接客户端,设置键生存时间

 三:方法执行,查看打印结果

 

大体流程就是这样,下面总结一下自己实现该功能时遇到的一些问题

一:java.lang.NoSuchMethodError: org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier;)V

可能原因:spring-data-redis的版本和spring版本不兼容

网传 spring-data-redis 2的只支持spring5和spring boot2+ ,读者需要根据自己的开发环境,导入相应的版本依赖

二:java.lang.NoClassDefFoundError: redis/clients/util/Pool

可能原因:jedis版本过高,请尝试降低版本,查看是否继续报错

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值