//package com.mortals.xhx.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import lombok.Data;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.Cache;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.interceptor.CacheErrorHandler;
//import org.springframework.cache.interceptor.SimpleCacheErrorHandler;
//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.Jackson2JsonRedisSerializer;
//
///**
// * @Author: linqj
// * @Date: 2019/3/6 10:36
// */
//@Data
//@Configuration
//public class RedisCacheConfig extends CachingConfigurerSupport {
//
//    /**
//     * 配置文件设置redis配置之后自动生成连接工厂类(使用lettuce)
//     */
//    @Autowired
//    private RedisConnectionFactory redisConnectionFactory;
//
//    /**
//     * 生成RedisTemplate
//     *
//     * @return
//     */
//    @Bean("redisTemplate")
//    public RedisTemplate redisTemplate() {
//        Jackson2JsonRedisSerializer<Object> 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);
//        RedisTemplate redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
////    @Bean("redisCacheManager")
////    public RedisCacheManager redisCacheManager() {
////        RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory);
////        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
////        // 设置默认缓存过期时间
////        if (!StringUtils.isEmpty(expiresTime)) {
////            config.entryTtl(Duration.ofMinutes(Long.valueOf(expiresTime))).disableCachingNullValues();
////        } else {
////            config.entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();
////        }
////        // 设置缓存空间
////        if (StringUtils.isEmpty(names)) {
////            names = "default-redis";
////        }
////        // 使用自定义配置初始化cacheManager
////        RedisCacheManager redisCacheManager = new RedisCacheManager(writer, config, names.split(","));
////        return redisCacheManager;
////    }
//
//
//
//    /**
//     * 如果cache出错， 我们会记录在日志里，方便排查，比如反序列化异常
//     */
//    @Override
//    public CacheErrorHandler errorHandler() {
//        return new LoggingCacheErrorHandler();
//    }
//
//    static class LoggingCacheErrorHandler extends SimpleCacheErrorHandler {
//        private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
//
//        @Override
//        public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
//            LOGGER.error(String.format("cacheName:%s,cacheKey:%s",
//                    cache == null ? "unknown" : cache.getName(), key), exception);
//            super.handleCacheGetError(exception, cache, key);
//        }
//
//        @Override
//        public void handleCachePutError(RuntimeException exception, Cache cache, Object key,
//                                        Object value) {
//            LOGGER.error(String.format("cacheName:%s,cacheKey:%s",
//                    cache == null ? "unknown" : cache.getName(), key), exception);
//            super.handleCachePutError(exception, cache, key, value);
//        }
//
//        @Override
//        public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
//            LOGGER.error(String.format("cacheName:%s,cacheKey:%s",
//                    cache == null ? "unknown" : cache.getName(), key), exception);
//            super.handleCacheEvictError(exception, cache, key);
//        }
//
//        @Override
//        public void handleCacheClearError(RuntimeException exception, Cache cache) {
//            LOGGER.error(String.format("cacheName:%s", cache == null ? "unknown" : cache.getName()),
//                    exception);
//            super.handleCacheClearError(exception, cache);
//        }
//    }
//
//}
