package com.docus.server.common.config;

import com.docus.server.common.util.RedisKeyExpirationListener;
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.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Bean
    public RedisKeyExpirationListener redisKeyExpirationListener() {
        return new RedisKeyExpirationListener(stringRedisTemplate);
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(){
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        // 监听 __keyevent@0__:expired ,使用键时间监听key的过期
        redisMessageListenerContainer.addMessageListener(redisKeyExpirationListener(),
                new PatternTopic("__keyevent@*__:expired"));

        return redisMessageListenerContainer;
    }



}
