content: package com.lap.rwlock.config;
  
  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.connection.RedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.data.redis.serializer.RedisSerializer;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  /**
   * @AUTHOR lgp
   * @DATE 2018/11/27 15:27
   * @DESCRIPTION
   **/
  @Configuration
  @EnableCaching
  public class RedisConfig extends CachingConfigurerSupport {
  
      @Bean
      @Override
      public KeyGenerator keyGenerator() {
          return (target, method, params) -> {
              StringBuilder sb = new StringBuilder();
              sb.append(target.getClass().getName());
              sb.append(method.getName());
              for (Object obj : params) {
                  sb.append(obj.toString());
              }
              return sb.toString();
          };
      }
  
      @Bean(name = "redisTemplate")
      public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
          RedisTemplate<String, Object> template = new RedisTemplate<>();
          template.setConnectionFactory(connectionFactory);
          setSerializer(template);
          template.afterPropertiesSet();
          return template;
      }
  
      /**
       * 设置序列化方法
       * lgp
       */
      private void setSerializer(RedisTemplate redisTemplate) {
          RedisSerializer stringSerializer = new StringRedisSerializer();
          redisTemplate.setKeySerializer(stringSerializer);
          redisTemplate.setValueSerializer(stringSerializer);
          redisTemplate.setHashKeySerializer(stringSerializer);
          redisTemplate.setHashValueSerializer(stringSerializer);
      }
  
  }
  