赞
踩
转自:https://www.cnblogs.com/wangyunhong/articles/16505079.html
1.打开conf/redis.conf 文件,取消注释:notify-keyspace-events Ex
2.重启redis
3.如果设置了密码需要重置密码:config set requirepass ****
3.验证配置是否生效
步骤一:进入redis客户端:redis-cli
步骤二:执行 CONFIG GET notify-keyspace-events ,如果有返回值证明配置成功,如果没有执行步骤三
步骤三:执行CONFIG SET notify-keyspace-events "Ex",再查看步骤二是否有值
注意:重置密码和重置配置是否每次重启redis都需要重新设置看个人需要。
- package com.gf.ecrm.redislistenerconfig;
-
- 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;
-
- import javax.annotation.Resource;
-
- @Configuration
- public class RedisListenerConfig {
-
- @Resource
- private RedisConnectionFactory redisConnectionFactory;
- @Resource
- private RedisKeyExpirationListener redisExpiredListener;
-
- @Bean
- public RedisMessageListenerContainer redisMessageListenerContainer() {
- RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
- redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
- //监听所有key的过期事件
- redisMessageListenerContainer.addMessageListener(redisExpiredListener, redisExpiredListener.getTopic());
- return redisMessageListenerContainer;
- }
-
- }
- package com.gf.ecrm.redislistenerconfig;
-
- import lombok.Data;
- import org.springframework.data.redis.connection.Message;
- import org.springframework.data.redis.connection.MessageListener;
- import org.springframework.data.redis.listener.PatternTopic;
- import org.springframework.stereotype.Component;
-
- @Data
- @Component
- public class RedisKeyExpirationListener implements MessageListener {
- //监听的主题(只监听redis数据库1,如果要监听redis所有的库,把1替换为*)
- public final PatternTopic topic = new PatternTopic("__keyevent@1__:expired");
-
- /**
- * Redis失效事件 key
- *
- * @param message
- * @param pattern
- */
- @Override
- public void onMessage(Message message, byte[] pattern) {
- String expiraKey = message.toString();
- System.out.println(expiraKey);
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。