|
@@ -0,0 +1,49 @@
|
|
|
+package org.hsweb.concureent.cache;
|
|
|
+
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
+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.JdkSerializationRedisSerializer;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by zhouhao on 16-4-26.
|
|
|
+ */
|
|
|
+@EnableCaching
|
|
|
+@Configuration
|
|
|
+public class RedisCacheConfig extends CachingConfigurerSupport {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public KeyGenerator wiselyKeyGenerator() {
|
|
|
+ return (target, method, params) -> {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(target.getClass().getName());
|
|
|
+ sb.append(method.getName());
|
|
|
+ for (Object obj : params) {
|
|
|
+ if (obj == null) obj = "null";
|
|
|
+ sb.append(obj.hashCode());
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CacheManager cacheManager(RedisTemplate redisTemplate) {
|
|
|
+ return new RedisCacheManager(redisTemplate);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisTemplate<String, String> redisTemplate(
|
|
|
+ RedisConnectionFactory factory) {
|
|
|
+ StringRedisTemplate template = new StringRedisTemplate(factory);
|
|
|
+ template.setValueSerializer(new JdkSerializationRedisSerializer());
|
|
|
+ return template;
|
|
|
+ }
|
|
|
+}
|