content: package com.zyh432.cache.redis.test.config;
  
  import com.zyh432.core.constants.DataStoragePlatformConstants;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.boot.SpringBootConfiguration;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.data.redis.cache.RedisCacheConfiguration;
  import org.springframework.data.redis.cache.RedisCacheManager;
  import org.springframework.data.redis.cache.RedisCacheWriter;
  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.StringRedisSerializer;
  
  /**
   * Redis Cache的配置类
   * 该缓存方案支持事务
   * 该缓存方案直接集成spring-boot-starter-data-redis，所以舍弃自定义配置，直接默认使用spring的配置
   */
  @SpringBootConfiguration
  @EnableCaching
  @Slf4j
  @ComponentScan(value = DataStoragePlatformConstants.BASE_COMPONENT_SCAN_PATH + ".cache.redis.test")
  public class RedisCacheConfig {
  
      /**
       * 定制链接和操作Redis的客户端工具
       *
       * @param redisConnectionFactory
       * @return
       */
      @Bean
      public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
          Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
          StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
          RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  
          redisTemplate.setConnectionFactory(redisConnectionFactory);
          redisTemplate.setKeySerializer(stringRedisSerializer);
          redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
          redisTemplate.setHashKeySerializer(stringRedisSerializer);
          redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  
          return redisTemplate;
      }
  
      /**
       * 定制化redis的缓存管理器
       *
       * @param redisConnectionFactory
       * @return
       */
      @Bean
      public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
          RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
                  .defaultCacheConfig()
                  .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                  .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)));
  
          RedisCacheManager cacheManager = RedisCacheManager
                  .builder(RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory))
                  .cacheDefaults(redisCacheConfiguration)
                  .transactionAware()
                  .build();
  
          log.info("the redis cache manager is loaded successfully!");
          return cacheManager;
      }
  
  
  }
  