项目需求:redis缓存的数据生存时间过期,提醒用户去更新缓存
1、开启Reids通知功能的配置
方法一:修改redis/redis.conf配置文件,添加如下设置 (永久有效)
1
|
notify-keyspace-events Ex
|
方法二:连接redis客户端,执行如下语句 (重启redis后失效)
1
|
config
set
notify-keyspace-events Ex
|
tip : Redis Config Set 命令可以动态地调整 Redis 服务器的配置(configuration),执行后无需重启,直接生效。
2、添加maven依赖 (注意版本问题)
1
2
3
4
5
6
7
8
9
|
<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>
|
3、Redis配置
一:redis.properties
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
#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
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?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>
|
4、自定义消息处理类
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
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));
}
}
|
大体流程就是这样,下面总结一下自己实现该功能时遇到的一些问题
一: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版本过高,请尝试降低版本,查看是否继续报错