package com.zd.laboratory.config;

import com.zd.laboratory.event.RedisExpiredAndAddListener;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * redis 监听配置
 * @Author: zhoupan
 * @Date: 2021/10/20/10:16
 * @Description:
 */
@Configuration
public class RedisListenerConfig {


//    @Autowired
//    private RedisUpdateAndAddListener redisUpdateAndAddListener;

//    @Autowired
//    private RedisDelAndAddListener redisDelAndAddListener;

    @Autowired
    private RedisExpiredAndAddListener redisExpiredAndAddListener;

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //监听所有的key的set事件
//        container.addMessageListener(redisUpdateAndAddListener, redisUpdateAndAddListener.getTopic());
        //container.addMessageListener(new RedisExpiredListener(), new PatternTopic("__keyevent@0__:expired"));
//        container.addMessageListener(redisDelAndAddListener, redisDelAndAddListener.getTopic());
        //过期事件
        container.addMessageListener(redisExpiredAndAddListener, redisExpiredAndAddListener.getTopic());
        return container;
    }
}
