content: package com.us.example.config;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.cache.Cache;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.annotation.CachingConfigurerSupport;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.cache.interceptor.CacheErrorHandler;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.data.redis.cache.RedisCacheManager;
  import org.springframework.data.redis.connection.RedisConnectionFactory;
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  /**
   * Created by yangyibo on 17/1/16.
   */
  @Configuration
  @EnableCaching
  public class RedisConfig extends CachingConfigurerSupport{
      private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
  
      @Autowired
      private Environment env;
  
      @Bean
      public JedisConnectionFactory redisConnectionFactory() {
          JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
          redisConnectionFactory.setHostName(env.getProperty("redis.hostname"));
          redisConnectionFactory.setPort(Integer.parseInt(env.getProperty("redis.port")));
          return redisConnectionFactory;
      }
  
      @Bean
      public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
          RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
          redisTemplate.setConnectionFactory(cf);
          return redisTemplate;
      }
  
      @Bean
      public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
          RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
          cacheManager.setDefaultExpiration(600);
          return cacheManager;
      }
  
      public CacheErrorHandler errorHandler() {
          return new CacheErrorHandler(){
  
              @Override
              public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
                  logger.warn("handleCacheGetError in redis: {}", exception.getMessage());
              }
  
              @Override
              public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
                  logger.warn("handleCachePutError in redis: {}", exception.getMessage());
              }
  
              @Override
              public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
                  logger.warn("handleCacheEvictError in redis: {}", exception.getMessage());
              }
  
              @Override
              public void handleCacheClearError(RuntimeException exception, Cache cache) {
                  logger.warn("handleCacheClearError in redis: {}", exception.getMessage());
              }};
      }
  }
  