content: package com.neroarc.blog.myblog.config;
  
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.springframework.cache.annotation.CachingConfigurerSupport;
  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.StringRedisSerializer;
  
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * @author: fjx
   * @date: 2019/11/29 21:42
   * Descripe:
   */
  
  @Configuration
  public class RedisCacheConfig extends CachingConfigurerSupport {
  
  
      @Bean
      public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
          RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
          //设置默认的过期时间(以秒为单位)
          //redisCacheManager.setDefaultExpiration(600);
  
          redisCacheManager.setUsePrefix(true);
  
          Map<String, Long> timeToLiveMap = new HashMap<>(2);
          //设置时间为12h
          timeToLiveMap.put("playList",43200L);
          //设置缓存区域(按key)的过期时间(以秒为单位)
          redisCacheManager.setExpires(timeToLiveMap);
          return redisCacheManager;
      }
  
  
      @Bean
      public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
          RedisTemplate<Object, Object> template = new RedisTemplate<>();
          template.setConnectionFactory(connectionFactory);
  
          //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式）
          Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
  
          ObjectMapper mapper = new ObjectMapper();
          //mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
          //mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  
          serializer.setObjectMapper(mapper);
  
          template.setValueSerializer(serializer);
          //使用StringRedisSerializer来序列化和反序列化redis的key值
          template.setKeySerializer(new StringRedisSerializer());
          template.afterPropertiesSet();
          return template;
      }
  
  
      @Bean
      public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
          StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
          stringRedisTemplate.setConnectionFactory(factory);
          return stringRedisTemplate;
      }
  
  }
  