diff --git a/hsweb-web-concurrent/hsweb-web-concurrent-cache/pom.xml b/hsweb-web-concurrent/hsweb-web-concurrent-cache/pom.xml new file mode 100644 index 000000000..fafaa05c2 --- /dev/null +++ b/hsweb-web-concurrent/hsweb-web-concurrent-cache/pom.xml @@ -0,0 +1,29 @@ + + + + hsweb-web-concurrent + org.hsweb + 1.0-SNAPSHOT + + 4.0.0 + hsweb-web-concurrent-cache + + + + org.springframework.boot + spring-boot-starter-redis + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.session + spring-session-data-redis + + + \ No newline at end of file diff --git a/hsweb-web-concurrent/hsweb-web-concurrent-cache/src/main/java/org/hsweb/concureent/cache/RedisCacheConfig.java b/hsweb-web-concurrent/hsweb-web-concurrent-cache/src/main/java/org/hsweb/concureent/cache/RedisCacheConfig.java new file mode 100644 index 000000000..f08889b64 --- /dev/null +++ b/hsweb-web-concurrent/hsweb-web-concurrent-cache/src/main/java/org/hsweb/concureent/cache/RedisCacheConfig.java @@ -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 redisTemplate( + RedisConnectionFactory factory) { + StringRedisTemplate template = new StringRedisTemplate(factory); + template.setValueSerializer(new JdkSerializationRedisSerializer()); + return template; + } +} \ No newline at end of file