package com.ltgk.framework.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @Author: wan kun
 * @Description: 监听redis key 过期事件
 * @Date: 2025/1/8 17:04
 * @Version: 1.0
 */
@Slf4j
@Component
public class RedisKeyExpirationListener implements MessageListener {
    private final RocketMQTemplate rocketMQTemplate;
    private final String expiredKeyTopic = "expiredKeyTopic";

    public RedisKeyExpirationListener(RocketMQTemplate rocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;

    }

    public void onMessage(Message message, byte[] pattern) {
        // 这里处理键过期事件
        String expiredKey = new String(message.getBody());
        // 推入到mq中  以防止 并发造成的key无法删除
        this.rocketMQTemplate.sendOneWay(this.expiredKeyTopic, expiredKey);
    }
}
