Skip to content

Commit 6042b5c

Browse files
author
Murtuza Ranapurwala
committed
Adds Spring Cloud AWS for caching
1 parent 10e8867 commit 6042b5c

File tree

11 files changed

+109
-237
lines changed

11 files changed

+109
-237
lines changed

spring-boot/caching-with-elasticache-redis/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ dependencies {
2424
implementation 'org.springframework.boot:spring-boot-starter-web'
2525
implementation 'org.springframework.boot:spring-boot-starter-cache'
2626
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
27+
implementation 'io.awspring.cloud:spring-cloud-starter-aws'
28+
// implementation 'com.amazonaws:aws-java-sdk-cloudformation'
29+
implementation 'com.amazonaws:aws-java-sdk-elasticache'
2730
compileOnly 'org.projectlombok:lombok'
2831
runtimeOnly 'com.h2database:h2'
2932
annotationProcessor 'org.projectlombok:lombok'
3033
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3134
}
3235

36+
dependencyManagement {
37+
imports {
38+
mavenBom 'io.awspring.cloud:spring-cloud-aws-dependencies:2.3.1'
39+
}
40+
}
41+
3342
test {
3443
useJUnitPlatform()
3544
}
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
package io.reflectoring.springcloudredis.configuration;
22

3-
import io.lettuce.core.ReadFrom;
4-
import org.springframework.beans.factory.annotation.Value;
53
import org.springframework.cache.annotation.EnableCaching;
6-
import org.springframework.context.annotation.Bean;
74
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.data.redis.connection.RedisStaticMasterReplicaConfiguration;
9-
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
10-
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
115

126
@Configuration
137
@EnableCaching
148
public class EnableCache {
15-
public static final int PORT = 6379;
16-
17-
@Value("${spring.redis.primary}")
18-
private String primaryEndpoint;
19-
20-
@Value("${spring.redis.reader}")
21-
private String readerEndpoint;
22-
23-
@Bean
24-
public LettuceConnectionFactory redisConnectionFactory() {
25-
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
26-
.readFrom(ReadFrom.REPLICA_PREFERRED)
27-
.build();
28-
var staticMasterReplicaConfiguration = new RedisStaticMasterReplicaConfiguration(this.primaryEndpoint, PORT);
29-
staticMasterReplicaConfiguration.addNode(readerEndpoint, PORT);
30-
return new LettuceConnectionFactory(staticMasterReplicaConfiguration, clientConfig);
31-
}
329

3310
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.reflectoring.springcloudredis.controller;
2+
3+
import io.reflectoring.springcloudredis.entity.User;
4+
import io.reflectoring.springcloudredis.service.UserService;
5+
import lombok.AllArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@Slf4j(topic = "PRODUCT_CONTROLLER")
13+
@RestController
14+
@RequestMapping("/user")
15+
@AllArgsConstructor
16+
public class UserController {
17+
18+
private final UserService userService;
19+
20+
@GetMapping("/{id}")
21+
public User getUser(@PathVariable String id) {
22+
return userService.getUser(id);
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.reflectoring.springcloudredis.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import java.io.Serializable;
9+
10+
@Entity
11+
@Getter
12+
@Setter
13+
public class User implements Serializable {
14+
@Id
15+
private String id;
16+
17+
private String firstName;
18+
19+
private String lastName;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.reflectoring.springcloudredis.repository;
2+
3+
import io.reflectoring.springcloudredis.entity.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface UserRepository extends JpaRepository<User, String> {
9+
}

spring-boot/caching-with-elasticache-redis/src/main/java/io/reflectoring/springcloudredis/service/ProductService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
@Service
1717
@AllArgsConstructor
18-
@CacheConfig(cacheNames = "product")
18+
@CacheConfig(cacheNames = "product-cache")
1919
public class ProductService {
2020
private final ProductRepository repository;
2121

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.reflectoring.springcloudredis.service;
2+
3+
import io.reflectoring.springcloudredis.entity.User;
4+
import io.reflectoring.springcloudredis.repository.UserRepository;
5+
import lombok.AllArgsConstructor;
6+
import org.springframework.cache.annotation.CacheConfig;
7+
import org.springframework.cache.annotation.Cacheable;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
@AllArgsConstructor
12+
@CacheConfig(cacheNames = "product-cache")
13+
public class UserService {
14+
15+
private final UserRepository repository;
16+
17+
@Cacheable
18+
public User getUser(String id){
19+
return repository.findById(id).orElseThrow(()->
20+
new RuntimeException("No such user found with id"));
21+
}
22+
}

spring-boot/caching-with-elasticache-redis/src/main/resources/application.yaml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ spring:
33
initialization-mode: always
44
application:
55
name: spring-cloud-redis
6-
cache:
7-
type: redis
8-
redis:
9-
time-to-live: 60000
10-
redis:
11-
primary: spring-redis-cluster.xxx.ng.0001.use2.cache.amazonaws.com
12-
reader: spring-redis-cluster-ro.xxx.ng.0001.use2.cache.amazonaws.com
136
jpa:
147
show-sql: true
158
generate-ddl: true
169
hibernate:
1710
ddl-auto: create-drop
18-
# redis:
19-
# cluster:
20-
# nodes: spring-redis-cluster.xxx.clustercfg.use2.cache.amazonaws.com:6379
21-
# lettuce:
22-
# cluster:
23-
# refresh:
24-
# period: PT10M
11+
#cloud:
12+
# aws:
13+
# stack:
14+
# name: spring-cache-2
15+
cloud:
16+
aws:
17+
elasticache:
18+
clusters:
19+
-
20+
name: product-cache
21+
expiration: 100
22+
-
23+
name: user-cache
24+
expiration: 6000

spring-boot/caching-with-elasticache-redis/src/main/resources/data.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@ insert into product
2727
(id, name, price, manufacturing_date, weight, height, width, category)
2828
values
2929
('prod_6', 'zara jacket green color', 1500, now(), 1.3, 10.4, 29.5, 'MEN_FASHION');
30+
31+
insert into user
32+
(id, first_name, last_name)
33+
values
34+
('user_1', 'john', 'doe');
35+
36+
insert into user
37+
(id, first_name, last_name)
38+
values
39+
('user_2', 'richard', 'mayor');

spring-boot/caching-with-elasticache-redis/terraform/inputs.tf

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)