package cn.reghao.tnb.data.app.mq;

import cn.reghao.tnb.content.api.dto.VideoCard;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author reghao
 * @date 2023-12-15 17:05:12
 */
@Configuration
public class RedisConfiguration {
    /*@Bean
    public ReactiveRedisOperations<String, VideoCard> eventRedisOperations(
            ReactiveRedisConnectionFactory redisConnectionFactory, ObjectMapper objectMapper) {
        

            return new ReactiveRedisTemplate<>(redisConnectionFactory,
                    RedisSerializationContext.<String, VideoCard>newSerializationContext()
                            .key(RedisSerializer.string())
                            .value(RedisSerializer.java())
                            .hashKey(RedisSerializer.string())
                            .hashValue(jsonRedisSerializer)
                            .build());
    }*/

    @Bean
    public RedisSerializationContext redisSerializationContext() {
        RedisSerializationContext.RedisSerializationContextBuilder builder = RedisSerializationContext.newSerializationContext();
        builder.key(StringRedisSerializer.UTF_8);
        builder.value(RedisSerializer.java());
        builder.hashKey(StringRedisSerializer.UTF_8);
        builder.hashValue(StringRedisSerializer.UTF_8);
        return builder.build();
    }

    @Bean
    public ReactiveRedisTemplate reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) {
        RedisSerializationContext serializationContext = redisSerializationContext();
        ReactiveRedisTemplate reactiveRedisTemplate = new ReactiveRedisTemplate(connectionFactory, serializationContext);
        return reactiveRedisTemplate;
    }
}
