package com.ytpm.middle.monitor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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;

/**
 * redis 监听器
 */
@Slf4j
@Component
@RefreshScope
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
 
    /**
     * Creates new {@link } for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
 
    /**
     * 监听redis过期的 key 进行处理
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key = String.valueOf(message);
        System.err.println("发现过期的key："+key);
    }
}