Last active
June 3, 2017 07:48
-
-
Save guilherfp/4b58620dfae4a273a3335fb284fbe698 to your computer and use it in GitHub Desktop.
Config Cache Redis
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
public class CacheConfiguration extends CachingConfigurerSupport { | |
@Bean | |
public JedisConnectionFactory redisConnectionFactory() { | |
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); | |
connectionFactory.setHostName("127.0.0.1"); | |
connectionFactory.setPort(6379); | |
return connectionFactory; | |
} | |
@Bean | |
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) { | |
RedisTemplate<String, String> template = new RedisTemplate<String, String>(); | |
template.setConnectionFactory(connectionFactory); | |
template.setDefaultSerializer(serializer()); | |
template.setEnableDefaultSerializer(true); | |
return template; | |
} | |
private Jackson2JsonRedisSerializer<Object> serializer() { | |
Jackson2JsonRedisSerializer<Object> serial = new Jackson2JsonRedisSerializer<>(Object.class); | |
FinObjectMapper objectMapper = new FinObjectMapper(); | |
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); | |
serial.setObjectMapper(objectMapper); | |
return serial; | |
} | |
@Bean | |
public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) { | |
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); | |
cacheManager.setDefaultExpiration(300); | |
return cacheManager; | |
} | |
} |
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
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-redis</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-pool2</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>redis.clients</groupId> | |
<artifactId>jedis</artifactId> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment