Last active
September 28, 2017 10:37
-
-
Save m-cakir/e3e7299d1599ccb844baca1b095fbda0 to your computer and use it in GitHub Desktop.
Medium post gist https://medium.com/@mcakir/spring-boot-redis-72c956612e3f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mcakir.bootiful.config; | |
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.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; | |
@EnableCaching | |
@Configuration | |
public class RedisConfiguration extends CachingConfigurerSupport { | |
@Autowired | |
CacheProperties cacheProperties; | |
private String KEY_SEPERATOR = "#"; | |
@Bean | |
@Override | |
public KeyGenerator keyGenerator() { | |
return new KeyGenerator() { | |
public Object generate(Object target, Method method, Object... params) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(target.getClass().getSimpleName()); | |
sb.append(KEY_SEPERATOR); | |
sb.append(method.getName()); | |
sb.append(KEY_SEPERATOR); | |
for(Object param : params){ | |
sb.append(param.toString()); | |
sb.append(KEY_SEPERATOR); | |
} | |
String str = sb.toString(); | |
return str.substring(0, str.length() - 1); | |
} | |
}; | |
} | |
@Bean | |
public CacheManager cacheManager(RedisTemplate redisTemplate) { | |
RedisCacheManager manager = new RedisCacheManager(redisTemplate); | |
manager.setExpires(cacheProperties.getExpirations()); | |
return manager; | |
} | |
@Bean | |
public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) { | |
final 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); | |
template.afterPropertiesSet(); | |
return template; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment