|
@@ -0,0 +1,130 @@
|
|
|
+package cn.rankin.cmsweb.configuration;
|
|
|
+
|
|
|
+import cn.rankin.common.utils.service.RedisService;
|
|
|
+import cn.rankin.common.utils.service.impl.RedisServiceImpl;
|
|
|
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
+import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+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.jedis.JedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableCaching //加上这个注解是的支持缓存注解
|
|
|
+public class RedisCacheConfig extends CachingConfigurerSupport {
|
|
|
+
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
+ private String host;
|
|
|
+
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
+ private int port;
|
|
|
+
|
|
|
+ @Value("${spring.redis.timeout}")
|
|
|
+ private int timeout;
|
|
|
+
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接redis的工厂类
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public JedisConnectionFactory jedisConnectionFactory() {
|
|
|
+ JedisConnectionFactory factory = new JedisConnectionFactory();
|
|
|
+ factory.setHostName(host);
|
|
|
+ factory.setPort(port);
|
|
|
+ factory.setTimeout(timeout);
|
|
|
+ factory.setPassword(password);
|
|
|
+ return factory;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置RedisTemplate
|
|
|
+ * 设置添加序列化器
|
|
|
+ * key 使用string序列化器
|
|
|
+ * value 使用Json序列化器
|
|
|
+ * 还有一种简答的设置方式,改变defaultSerializer对象的实现。
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean(name = "redisTemplate")
|
|
|
+ public RedisTemplate<Object, Object> redisTemplate() {
|
|
|
+ //StringRedisTemplate的构造方法中默认设置了stringSerializer
|
|
|
+ RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
|
|
+ //设置开启事务
|
|
|
+ template.setEnableTransactionSupport(true);
|
|
|
+ //set key serializer
|
|
|
+ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
+ template.setKeySerializer(stringRedisSerializer);
|
|
|
+ template.setHashKeySerializer(stringRedisSerializer);
|
|
|
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
+ objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
+
|
|
|
+ jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
|
|
+
|
|
|
+ //set value serializer
|
|
|
+// template.setDefaultSerializer(jackson2JsonRedisSerializer);
|
|
|
+ template.setValueSerializer(jackson2JsonRedisSerializer);
|
|
|
+ template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
|
|
+
|
|
|
+ template.setConnectionFactory(jedisConnectionFactory());
|
|
|
+ template.afterPropertiesSet();
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置RedisCacheManager
|
|
|
+ * 使用cache注解管理redis缓存
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public RedisCacheManager cacheManager() {
|
|
|
+ RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
|
|
|
+ return redisCacheManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisService redisService() {
|
|
|
+ RedisServiceImpl redisService = new RedisServiceImpl();
|
|
|
+ redisService.setRedisTemplate(this.redisTemplate());
|
|
|
+ return redisService;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义生成redis-key
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public KeyGenerator keyGenerator() {
|
|
|
+ return new KeyGenerator() {
|
|
|
+ @Override
|
|
|
+ public Object generate(Object o, Method method, Object... objects) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(o.getClass().getName()).append(".");
|
|
|
+ sb.append(method.getName()).append(".");
|
|
|
+ for (Object obj : objects) {
|
|
|
+ sb.append(obj.toString());
|
|
|
+ }
|
|
|
+ System.out.println("keyGenerator=" + sb.toString());
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|