content: package cn.edu.seig.vibemusic.config;
  
  import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.annotation.JsonTypeInfo;
  import com.fasterxml.jackson.annotation.PropertyAccessor;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.redis.cache.RedisCacheConfiguration;
  import org.springframework.data.redis.cache.RedisCacheManager;
  import org.springframework.data.redis.connection.RedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  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;
  
  import java.time.Duration;
  
  @Configuration
  public class RedisConfig {
  
      /**
       * 自定义 Jackson2JsonRedisSerializer 配置
       */
      private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
          ObjectMapper objectMapper = new ObjectMapper();
          objectMapper.registerModule(new JavaTimeModule());
          objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
          objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
                  ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
          return new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
      }
  
      /**
       * RedisTemplate 配置
       */
      @Bean
      public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          RedisTemplate<Object, Object> template = new RedisTemplate<>();
          template.setConnectionFactory(redisConnectionFactory);
          // 使用自定义的 Jackson2JsonRedisSerializer
          template.setDefaultSerializer(jackson2JsonRedisSerializer());
          return template;
      }
  
      /**
       * RedisCacheManager 配置
       */
      @Bean
      public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
          // Key 序列化器
          RedisSerializer<String> stringSerializer = new StringRedisSerializer();
          // Value 序列化器
          Jackson2JsonRedisSerializer<Object> valueSerializer = jackson2JsonRedisSerializer();
  
          // 配置缓存的序列化方式
          RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                  .entryTtl(Duration.ofHours(6)) // 缓存过期时间 6 小时
                  .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
                  .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer));
  
          return RedisCacheManager.builder(redisConnectionFactory)
                  .cacheDefaults(cacheConfig)
                  .build();
      }
  }
  