package com.nuliji.tools.cache;


//<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
//        <property name="hostName" value="${pom.redis.host}"/>
//        <property name="port" value="${pom.redis.port}"/>
//        <property name="password" value="${pom.redis.auth}"/>
//    </bean>
//    <bean class="org.springframework.data.redis.core.RedisTemplate" primary="true">
//        <property name="connectionFactory" ref="redisConnectionFactory"/>
//    </bean>
//    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
//        <property name="connectionFactory" ref="redisConnectionFactory"/>
//    </bean>

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.lang.reflect.Method;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public KeyGenerator keyGenerator() {
        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();
            }
        };
    }
}
