content: package com.jerry.poetry.config;
  
  import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.annotation.PropertyAccessor;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.annotation.CachingConfigurerSupport;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.cache.interceptor.KeyGenerator;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  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.core.StringRedisTemplate;
  import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.RedisSerializer;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  import java.lang.reflect.Method;
  
  @Configuration
  @EnableCaching
  public class RedisConfiguration extends CachingConfigurerSupport {
  
      /**
       * 自定义生成key的规则
       * @return
       */
      @Override
      public KeyGenerator keyGenerator() {
          return new KeyGenerator() {
              @Override
              public Object generate(Object o, Method method, Object... objects) {
                  //格式化缓存key字符串
                  StringBuilder sb = new StringBuilder();
                  //追加类名
                  sb.append(o.getClass().getName());
                  //追加方法名
                  sb.append(method.getName());
                  //遍历参数并且追加
                  for (Object obj : objects) {
                      sb.append(obj.toString());
                  }
                  System.out.println("调用Redis缓存Key : " + sb.toString());
                  return sb.toString();
              }
          };
      }
      /**
       * 采用RedisCacheManager作为缓存管理器
       * @param connectionFactory
       * @return
       */
      @Bean
      public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
          RedisCacheManager redisCacheManager = RedisCacheManager.create(connectionFactory);
          return  redisCacheManager;
      }
  
      @Bean
      public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
          //解决键、值序列化问题
          StringRedisTemplate template = new StringRedisTemplate(factory);
          Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
          ObjectMapper om = new ObjectMapper();
          om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
          om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
          jackson2JsonRedisSerializer.setObjectMapper(om);
          template.setValueSerializer(jackson2JsonRedisSerializer);
          template.afterPropertiesSet();
  
  //        RedisTemplate<String, String> template = new RedisTemplate<String, String>();
  //        template.setConnectionFactory(factory);
  //        //更改在redis里面查看key编码问题
  //        RedisSerializer redisSerializer =new StringRedisSerializer();
  //        template.setKeySerializer(redisSerializer);
  //        template.setValueSerializer(redisSerializer);
          return template;
      }
  
  }
  