数据库
首页 > 数据库> > 过期的键触发事件 – Spring数据Redis

过期的键触发事件 – Spring数据Redis

作者:互联网

我正在尝试使用Spring数据redis设置Key expiry事件处理程序,但我无法使其工作.我做了一些研究,但是给了预期的结果.不知道我错过了什么.我想在redis缓存上的密钥到期时记录消息.

@Configuration
@EnableRedisRepositories
public class RedisConfig {

    @Bean 
    StringRedisTemplate stringRedisTemplate(){
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());
        return stringRedisTemplate;
    }

    @Bean
    RedisMessageListenerContainer redisContainer() {
        RedisMessageListenerContainer container 
          = new RedisMessageListenerContainer(); 
        container.setConnectionFactory(jedisConnectionFactory()); 
        container.addMessageListener(messageListener(), topic()); 
        return container; 
    }

    @Bean
    MessageListenerAdapter messageListener() { 
        return new MessageListenerAdapter(new RedisMessageSubscriber());
    }

    @Bean
    MessagePublisher redisPublisher() { 
        return new RedisMessagePublisher();
    }

    @Bean
    ChannelTopic topic() {
        return new ChannelTopic("messageQueue");
    }
}


public interface MessagePublisher {
    void publish(String message);
}

public class RedisMessagePublisher implements MessagePublisher {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ChannelTopic topic;

    public void publish(String message) {
        stringRedisTemplate.convertAndSend(topic.getTopic(), message);
    }
}

@Service
public class RedisMessageSubscriber implements MessageListener {

    private static final Logger LOG = Logger.getLogger(RedisMessageSubscriber.class);

    public static List<String> messageList = new ArrayList<String>();

    @Override
    public void onMessage(Message message, byte[] pattern) {
        messageList.add(message.toString());
    }

}

有人可以帮忙吗?
谢谢

解决方法:

首先,需要在redis中启用expire事件的密钥空间通知.
默认情况下,禁用键空间事件通知,因为虽然不太明智,但该功能会使用一些CPU电源.使用redis.conf的notify-keyspace-events或CONFIG SET启用通知.

notify-keyspace-events "Ex"

完成redis后,需要使用模式主题“__keyevent @ * __:expired”配置消息监听器.这是代码示例.

听众课程:

@Component
public class ExpirationListener implements MessageListener {
    private static final Logger logger = LoggerFactory.getLogger(ExpirationListener.class);

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String key = new String(message.getBody());
        logger.debug("expired key: {}", key);
    }
}

组态:

@Bean
RedisMessageListenerContainer keyExpirationListenerContainer(RedisConnectionFactory connectionFactory, ExpirationListener expirationListener) {
    RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
    listenerContainer.setConnectionFactory(connectionFactory);
    listenerContainer.addMessageListener(expirationListener, new PatternTopic("__keyevent@*__:expired"));
    listenerContainer.setErrorHandler(e -> logger.error("There was an error in redis key expiration listener container", e));
    return listenerContainer;
}

标签:spring,redis,spring-data,spring-data-redis
来源: https://codeday.me/bug/20190622/1262719.html