content: package cc.langhai.config.redis;
  
  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.RedisTemplate;
  import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.RedisSerializer;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  /**
   * redis配置类
   *
   * @author langhai
   * @date 2022-11-22 21:54
   */
  @Configuration
  public class MyRedisConfig {
  
      @Bean(name = "redisTemplate")
      public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
          // 参照StringRedisTemplate内部实现指定序列化器
          redisTemplate.setConnectionFactory(redisConnectionFactory);
          redisTemplate.setKeySerializer(keySerializer());
          redisTemplate.setHashKeySerializer(keySerializer());
          redisTemplate.setValueSerializer(valueSerializer());
          redisTemplate.setHashValueSerializer(valueSerializer());
          return redisTemplate;
      }
  
      private RedisSerializer<String> keySerializer() {
          return new StringRedisSerializer();
      }
  
      /**
       * 使用Jackson序列化器
       *
       * @return RedisSerializer
       */
      private RedisSerializer<Object> valueSerializer() {
          return new GenericJackson2JsonRedisSerializer();
      }
  }