Created
March 23, 2019 18:38
-
-
Save MarounMaroun/55833ae139dc2038cc9b86ec2f06ffa7 to your computer and use it in GitHub Desktop.
Spring 2.x Redis configurations
This file contains hidden or 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
import java.time.Duration; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.cache.CacheManager; | |
import org.springframework.cache.annotation.CachingConfigurerSupport; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.redis.cache.RedisCacheConfiguration; | |
import org.springframework.data.redis.cache.RedisCacheManager; | |
import org.springframework.data.redis.cache.RedisCacheWriter; | |
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | |
/** | |
* Cache configurations. | |
* | |
* @author Maroun Maroun | |
* @since 2019/03/21 | |
*/ | |
@Configuration | |
@EnableCaching | |
public class CacheConfiguration extends CachingConfigurerSupport { | |
private final Logger logger = LoggerFactory.getLogger(getClass()); | |
@Value("${spring.redis.host}") | |
private String redisHost; | |
@Value("${spring.redis.port}") | |
private int redisPort; | |
@Value("${spring.cache.redis.time-to-live}") | |
private long ttl; | |
// default cache: Redis | |
@Bean | |
@Override | |
public CacheManager cacheManager() { | |
logger.info("Creating a Redis cache manager with TTL %d", ttl); | |
RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(jedisConnectionFactory()); | |
// note: there's no way of explicitly setting the use-prefix to be true, it can however | |
// be disabled using the .disableKeyPrefix() method | |
RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig() | |
.disableCachingNullValues() | |
.entryTtl(Duration.ofMillis(ttl)); | |
return new RedisCacheManager(writer, cacheConfig); | |
} | |
@Bean | |
JedisConnectionFactory jedisConnectionFactory() { | |
logger.info("Creating a Jedis connection factory on %s:%d", redisHost, redisPort); | |
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); | |
return new JedisConnectionFactory(config); | |
} | |
// Concurrent map cache manager | |
@Bean | |
public CacheManager concurrentMapCacheManager() { | |
return new ConcurrentMapCacheManager(); | |
} | |
} | |
// If cacheManager value on the @Cacheable is not specified, Redis will be used as default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment