赞
踩
package com.dgis.tis.master.util.redis; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * @description: redis监听器容器 * @create: 2022/07/08 */ @Configuration public class RedisListenerConfig { @Bean @Primary public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; } }
package com.dgis.tis.master.util.redis; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Component; /** * @description: key 过期监听器 * @create: 2022/07/08 */ @Component @Slf4j public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { private final RedisUtil redisUtil; public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer, RedisUtil redisUtil) { super(listenerContainer); this.redisUtil = redisUtil; } @Override public void onMessage(Message message, byte[] pattern) { // String key = new String(message.getBody()); String expiredKey = message.toString(); log.info("expiredKey:{}", expiredKey); if(expiredKey.startsWith("wxToken")){ log.info("wxToken已过期"); // 删除该key redisUtil.removeValue("wxToken"); } } }
redis 配置修改conf:
notify-keyspace-events Ex
注:只能获取到失效的 key 而获取不到 value,在设计 key 的时候可以考虑把 value 一并放到 key,然后根据规则取出 value
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。