Skip to content

Instantly share code, notes, and snippets.

@m-cakir
Last active September 28, 2017 10:37
Show Gist options
  • Save m-cakir/e3e7299d1599ccb844baca1b095fbda0 to your computer and use it in GitHub Desktop.
Save m-cakir/e3e7299d1599ccb844baca1b095fbda0 to your computer and use it in GitHub Desktop.
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