Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security-oauth2-client'
implementation 'com.google.api-client:google-api-client:2.7.2'
Expand All @@ -55,7 +55,6 @@ dependencies {
testRuntimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-actuator-test'
testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa-test'
// testImplementation 'org.springframework.boot:spring-boot-starter-data-redis-test'
testImplementation 'org.springframework.boot:spring-boot-starter-security-oauth2-client-test'
testImplementation 'org.springframework.boot:spring-boot-starter-security-test'
testImplementation 'org.springframework.boot:spring-boot-starter-validation-test'
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ services:
MAIL_USERNAME: ${MAIL_USERNAME}
MAIL_PASSWORD: ${MAIL_PASSWORD}

SPRING_DATA_REDIS_HOST: ${REDIS_HOST}
SPRING_DATA_REDIS_PORT: ${REDIS_PORT}
SPRING_DATA_REDIS_PASSWORD: ${REDIS_PASSWORD}

LOGGING_FILE_NAME: /app/logs/application.log

JAVA_OPTS: >-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class CategoryController implements CategoryApiDocs {

@Override
@GetMapping
// TODO: cache 처리
public ResponseEntity<ApiResponse<CategoryResponse>> getCategory() {
List<CategoryInfo> categoryInfos = categoryService.getCategory();
return ResponseEntity.ok(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final CategoryRepository categoryRepository;

@Cacheable(value = "categories")
@Transactional(readOnly = true)
public List<CategoryInfo> getCategory() {
List<Category> categories = categoryRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.rentify.rentify_api.common.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@EnableCaching
@Profile("local")
public class LocalCacheConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.rentify.rentify_api.common.config;

import java.time.Duration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

@Configuration
@EnableCaching
@Profile("dev")
public class RedisConfig {

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
JacksonJsonRedisSerializer<Object> serializer =
new JacksonJsonRedisSerializer<>(Object.class);

RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.serializeValuesWith(
RedisSerializationContext.SerializationPair
.fromSerializer(serializer)
);

return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -66,6 +68,7 @@ public Page<PostDetailResponse> getPosts(
return posts.map(PostDetailResponse::from);
}

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

@CacheEvict(value = "posts", key = "#postId")
@Transactional
public Long updatePost(Long postId, Long userId, PostFormRequest request) {
Post post = postRepository.findById(postId)
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ spring:
keepalive-time: 300000
max-lifetime: 1740000

cache:
type: redis
data:
redis:
host: ${SPRING_DATA_REDIS_HOST}
port: ${SPRING_DATA_REDIS_PORT:6379}
password: ${SPRING_DATA_REDIS_PASSWORD}

security:
oauth2:
client:
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ spring:
keepalive-time: 300000
max-lifetime: 1740000

cache:
type: simple

security:
oauth2:
client:
Expand Down