- Design
- Configuration
- Redis Connection
- Implementation
In this post, the cache implementation will be done through spring boot. So Im going to have spring aspect which enabled around aspect so that, itl enable us to interact with cache before and after target method execution.
For this implementation, I have already set up Redis cluster in AWS and it'l be enable us to connect to cluster through primary node.
Configuration
Add below dependency in to you pom.xml,
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Redis Connection
Add below connection properties in to you Spring boot properties yaml file
spring:redis:
clusterurl: {redus_cluster_url}
timeout: 5000
lettuce:
pool:
max-active: 10
max-wait: -1
max-idle: 50
min-idle: 0
Create connection configuration in your application as below
@Value("${spring.redis.clusterurl}")
private String redisClusterNodes;
@Bean(value = "lettuceConnectionFactory")
@Primary
LettuceConnectionFactory lettuceConnectionFactory() {
LOGGER.info("Redis cluster url: " + redisClusterNodes); List
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().useSsl().and()
.commandTimeout(Duration.ofSeconds(3)).shutdownTimeout(Duration.ZERO).build();
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(nodes);
LettuceConnectionFactory lcf = new LettuceConnectionFactory(redisClusterConfiguration, clientConfig);
lcf.afterPropertiesSet(); return lcf;
}
Implementation
Refer the source code attached for implementation. Where ever you use the EnableCache annotation, you need to include below component scan.
@ComponentScan(basePackages = { "com.cache.redis.config"})
Enable Cache to the method
@EnableCache
public String put(String payload, Map
Make sure to pass cacheparam as a last method parameter as below
Sourcode: github
