Skip to content

Commit 002bfa2

Browse files
authored
Merge pull request #92 from Dalguring/feature/set-redis
Feature/set redis
2 parents ad6dc08 + 2870570 commit 002bfa2

9 files changed

Lines changed: 69 additions & 3 deletions

File tree

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repositories {
3131
dependencies {
3232
implementation 'org.springframework.boot:spring-boot-starter-actuator'
3333
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
34-
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
34+
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
3535
implementation 'org.springframework.boot:spring-boot-starter-security'
3636
implementation 'org.springframework.boot:spring-boot-starter-security-oauth2-client'
3737
implementation 'com.google.api-client:google-api-client:2.7.2'
@@ -55,7 +55,6 @@ dependencies {
5555
testRuntimeOnly 'com.h2database:h2'
5656
testImplementation 'org.springframework.boot:spring-boot-starter-actuator-test'
5757
testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa-test'
58-
// testImplementation 'org.springframework.boot:spring-boot-starter-data-redis-test'
5958
testImplementation 'org.springframework.boot:spring-boot-starter-security-oauth2-client-test'
6059
testImplementation 'org.springframework.boot:spring-boot-starter-security-test'
6160
testImplementation 'org.springframework.boot:spring-boot-starter-validation-test'

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ services:
5151
MAIL_USERNAME: ${MAIL_USERNAME}
5252
MAIL_PASSWORD: ${MAIL_PASSWORD}
5353

54+
SPRING_DATA_REDIS_HOST: ${REDIS_HOST}
55+
SPRING_DATA_REDIS_PORT: ${REDIS_PORT}
56+
SPRING_DATA_REDIS_PASSWORD: ${REDIS_PASSWORD}
57+
5458
LOGGING_FILE_NAME: /app/logs/application.log
5559

5660
JAVA_OPTS: >-

src/main/java/com/rentify/rentify_api/category/controller/CategoryController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class CategoryController implements CategoryApiDocs {
2121

2222
@Override
2323
@GetMapping
24-
// TODO: cache 처리
2524
public ResponseEntity<ApiResponse<CategoryResponse>> getCategory() {
2625
List<CategoryInfo> categoryInfos = categoryService.getCategory();
2726
return ResponseEntity.ok(

src/main/java/com/rentify/rentify_api/category/service/CategoryService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.stream.Collectors;
88
import lombok.RequiredArgsConstructor;
99
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.cache.annotation.Cacheable;
1011
import org.springframework.stereotype.Service;
1112
import org.springframework.transaction.annotation.Transactional;
1213

@@ -17,6 +18,7 @@ public class CategoryService {
1718

1819
private final CategoryRepository categoryRepository;
1920

21+
@Cacheable(value = "categories")
2022
@Transactional(readOnly = true)
2123
public List<CategoryInfo> getCategory() {
2224
List<Category> categories = categoryRepository.findAll();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.rentify.rentify_api.common.config;
2+
3+
import org.springframework.cache.annotation.EnableCaching;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Profile;
6+
7+
@Configuration
8+
@EnableCaching
9+
@Profile("local")
10+
public class LocalCacheConfig {
11+
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.rentify.rentify_api.common.config;
2+
3+
import java.time.Duration;
4+
import org.springframework.cache.annotation.EnableCaching;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
8+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
9+
import org.springframework.data.redis.cache.RedisCacheManager;
10+
import org.springframework.data.redis.connection.RedisConnectionFactory;
11+
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
12+
import org.springframework.data.redis.serializer.RedisSerializationContext;
13+
14+
@Configuration
15+
@EnableCaching
16+
@Profile("dev")
17+
public class RedisConfig {
18+
19+
@Bean
20+
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
21+
JacksonJsonRedisSerializer<Object> serializer =
22+
new JacksonJsonRedisSerializer<>(Object.class);
23+
24+
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
25+
.entryTtl(Duration.ofMinutes(10))
26+
.serializeValuesWith(
27+
RedisSerializationContext.SerializationPair
28+
.fromSerializer(serializer)
29+
);
30+
31+
return RedisCacheManager.builder(connectionFactory)
32+
.cacheDefaults(config)
33+
.build();
34+
}
35+
}

src/main/java/com/rentify/rentify_api/post/service/PostService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Set;
2020
import lombok.RequiredArgsConstructor;
2121
import lombok.extern.slf4j.Slf4j;
22+
import org.springframework.cache.annotation.CacheEvict;
23+
import org.springframework.cache.annotation.Cacheable;
2224
import org.springframework.data.domain.Page;
2325
import org.springframework.data.domain.Pageable;
2426
import org.springframework.data.domain.Sort;
@@ -66,6 +68,7 @@ public Page<PostDetailResponse> getPosts(
6668
return posts.map(PostDetailResponse::from);
6769
}
6870

71+
@Cacheable(value = "posts", key = "#postId")
6972
@Transactional(readOnly = true)
7073
public PostDetailResponse getPost(Long postId) {
7174
Post post = postRepository.findById(postId)
@@ -107,6 +110,7 @@ public Long createPost(Long userId, PostFormRequest request) {
107110
return savedPost.getId();
108111
}
109112

113+
@CacheEvict(value = "posts", key = "#postId")
110114
@Transactional
111115
public Long updatePost(Long postId, Long userId, PostFormRequest request) {
112116
Post post = postRepository.findById(postId)

src/main/resources/application-dev.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ spring:
1616
keepalive-time: 300000
1717
max-lifetime: 1740000
1818

19+
cache:
20+
type: redis
21+
data:
22+
redis:
23+
host: ${SPRING_DATA_REDIS_HOST}
24+
port: ${SPRING_DATA_REDIS_PORT:6379}
25+
password: ${SPRING_DATA_REDIS_PASSWORD}
26+
1927
security:
2028
oauth2:
2129
client:

src/main/resources/application-local.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ spring:
1616
keepalive-time: 300000
1717
max-lifetime: 1740000
1818

19+
cache:
20+
type: simple
21+
1922
security:
2023
oauth2:
2124
client:

0 commit comments

Comments
 (0)