package com.chinacreator.video.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;  
import com.fasterxml.jackson.annotation.PropertyAccessor;  
import com.fasterxml.jackson.databind.ObjectMapper;  

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;  
import org.springframework.cache.annotation.Cacheable;
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 java.lang.reflect.Method;  
import java.util.HashMap;
import java.util.Map;
  
  
@Configuration  
@EnableCaching  
public class RedisConfig extends CachingConfigurerSupport{
	
    @Autowired
    private MyStringSerializer myStringSerializer;
  
    @Bean  
    public KeyGenerator wiselyKeyGenerator(){  
		return new KeyGenerator() {  
		    @Override  
		    public Object generate(Object target, Method method, Object... 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  
//    public CacheManager cacheManager(  
//        @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {  
//        return new RedisCacheManager(redisTemplate);  
//    } 
    
    @Bean
    public RedisCacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        // 开启使用缓存名称最为key前缀
        redisCacheManager.setUsePrefix(true);
        //这里可以设置一个默认的过期时间 单位是秒，默认3分钟
        redisCacheManager.setDefaultExpiration(180);
     
        // 设置缓存的过期时间
        Map<String, Long> expires = new HashMap<>();
        expires.put("qryIpLimitConf", Long.parseLong(300+"")); //设置缓存名称为qryIpLimitConf的过期时间是300秒
        redisCacheManager.setExpires(expires);
     
        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); 
        //指定dao类使用@Cacheable时的key前缀，比如PointShopDao类的getPointShopConfig方法，生成的key是"pointshop:univideo_db_1168"，
        //格式为pointshop+“univideo_db_”+查询时输入的参数
        //template.setKeySerializer(myStringSerializer);
        template.afterPropertiesSet();  
        return template;  
    }  
}  