Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: SchoolFestivalsV1QueryService 추가 및 Spring Cache 적용 (#862) #864

Closed
wants to merge 7 commits into from
4 changes: 4 additions & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-mail")
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:${swaggerVersion}")

// Spring Security
Expand Down Expand Up @@ -79,6 +80,9 @@ dependencies {
annotationProcessor("org.projectlombok:lombok")
testCompileOnly("org.projectlombok:lombok")
testAnnotationProcessor("org.projectlombok:lombok")

// Caffeine
implementation("com.github.ben-manes.caffeine:caffeine")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.festago.common.cache;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class CacheInvalidator {

private final CacheManager cacheManager;

public void invalidate(String cacheName) {
Optional.ofNullable(cacheManager.getCache(cacheName))
.ifPresentOrElse(cache -> {
cache.invalidate();
log.info("{} 캐시를 초기화 했습니다.", cacheName);
}, () -> log.error("{} 캐시를 찾을 수 없습니다.", cacheName));
}
}
Comment on lines +9 to +23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

캐시 초기화를 담당하는 컴포넌트 입니다.
축제 캐싱이 30분 동안 유지되므로, 그 사이 새로운 축제가 추가되면 사용자가 축제 정보를 확인할 수 없는 문제가 생깁니다.
축제는 관리자가 추가하기에 큰 문제는 아니지만, 가끔 즉시 초기화가 필요한 시점이 있을 수 있기에 별도의 컴포넌트로 분리했습니다.
만약, 수동으로 초기화가 필요하다면 관리자 API를 열어서 직접 초기화하면 될 것 같습니다.

21 changes: 21 additions & 0 deletions backend/src/main/java/com/festago/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.festago.config;

import java.util.List;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager(List<Cache> caches) {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return cacheManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.festago.school.application.v1;

import com.festago.school.dto.v1.SchoolFestivalV1Response;
import com.festago.school.repository.v1.SchoolFestivalsV1QueryDslRepository;
import java.time.Clock;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SchoolFestivalsV1QueryService {

public static final String SCHOOL_FESTIVALS_V1_CACHE_NAME = "schoolFestivalsV1";
public static final String PAST_SCHOOL_FESTIVALS_V1_CACHE_NAME = "pastSchoolFestivalsV1";

private final SchoolFestivalsV1QueryDslRepository schoolFestivalsV1QueryDslRepository;
private final Clock clock;

@Cacheable(cacheNames = SCHOOL_FESTIVALS_V1_CACHE_NAME, key = "#schoolId")
public List<SchoolFestivalV1Response> findFestivalsBySchoolId(Long schoolId) {
LocalDate now = LocalDate.now(clock);
return schoolFestivalsV1QueryDslRepository.findFestivalsBySchoolId(schoolId, now);
}

@Cacheable(cacheNames = PAST_SCHOOL_FESTIVALS_V1_CACHE_NAME, key = "#schoolId")
public List<SchoolFestivalV1Response> findPastFestivalsBySchoolId(Long schoolId) {
LocalDate now = LocalDate.now(clock);
return schoolFestivalsV1QueryDslRepository.findPastFestivalsBySchoolId(schoolId, now);
}
}
Comment on lines +13 to +35
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchoolFestivalsV1QueryService에서 CacheName을 가지고 있습니다.
이유는 누군가 CacheName을 관리해야 하는데, 캐시 구현체인 SchoolFestivalsV1CacheConfig에서 관리하기엔 application 레이어인 Service에서 infrastructure에 대한 의존이 발생하더군요. 😂
따라서 캐시의 직접적인 사용자인 SchoolFestivalsV1QueryService에서 CacheName을 가지고 있도록 하였습니다.
CacheName이 문자열 + 불변하므로, public으로 노출되더라도 큰 문제는 없을 것 같다고 판단됩니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.festago.school.infrastructure;

import com.festago.school.application.v1.SchoolFestivalsV1QueryService;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SchoolFestivalsV1CacheConfig {

private static final long EXPIRED_AFTER_WRITE = 30;
private static final long MAXIMUM_SIZE = 1_000;

@Bean
public Cache schoolFestivalsV1Cache() {
return new CaffeineCache(SchoolFestivalsV1QueryService.SCHOOL_FESTIVALS_V1_CACHE_NAME,
Caffeine.newBuilder()
.recordStats()
.expireAfterWrite(EXPIRED_AFTER_WRITE, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build()
);
}

@Bean
public Cache pastSchoolFestivalsV1Cache() {
return new CaffeineCache(SchoolFestivalsV1QueryService.PAST_SCHOOL_FESTIVALS_V1_CACHE_NAME,
Caffeine.newBuilder()
.recordStats()
.expireAfterWrite(EXPIRED_AFTER_WRITE, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.festago.school.infrastructure;

import com.festago.common.cache.CacheInvalidator;
import com.festago.school.application.v1.SchoolFestivalsV1QueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SchoolFestivalsV1CacheInvalidateScheduler {

private final CacheInvalidator cacheInvalidator;

// 매일 정각마다 캐시 초기화
@Scheduled(cron = "0 0 0 * * *")
public void invalidate() {
cacheInvalidator.invalidate(SchoolFestivalsV1QueryService.SCHOOL_FESTIVALS_V1_CACHE_NAME);
cacheInvalidator.invalidate(SchoolFestivalsV1QueryService.PAST_SCHOOL_FESTIVALS_V1_CACHE_NAME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.festago.school.repository.v1;

import static com.festago.festival.domain.QFestival.festival;
import static com.festago.festival.domain.QFestivalQueryInfo.festivalQueryInfo;

import com.festago.common.querydsl.QueryDslHelper;
import com.festago.school.dto.v1.QSchoolFestivalV1Response;
import com.festago.school.dto.v1.SchoolFestivalV1Response;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class SchoolFestivalsV1QueryDslRepository {

private final QueryDslHelper queryDslHelper;

public List<SchoolFestivalV1Response> findFestivalsBySchoolId(
Long schoolId,
LocalDate today
) {
return queryDslHelper.select(
new QSchoolFestivalV1Response(
festival.id,
festival.name,
festival.startDate,
festival.endDate,
festival.thumbnail,
festivalQueryInfo.artistInfo
)
)
.from(festival)
.leftJoin(festivalQueryInfo).on(festivalQueryInfo.festivalId.eq(festival.id))
.where(festival.school.id.eq(schoolId).and(festival.endDate.goe(today)))
.stream()
.sorted(Comparator.comparing(SchoolFestivalV1Response::startDate))
.toList();
}
Comment on lines +21 to +41
Copy link
Collaborator Author

@seokjin8678 seokjin8678 Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB를 통해 정렬을 수행하지 않고, 어플리케이션 레벨에서 정렬을 수행하도록 했습니다.
이유는 조회 시 가져오는 축제의 개수가 매우 적기 때문에, 어플리케이션 레벨에서 정렬하는게 효율적이라 판단하였습니다.
실제 성능 비교를 해보면, 정말 미미한 차이겠지만.. 해당 구현으로 FESTIVAL 테이블에 index_festival_end_date_desc, index_festival_start_date 인덱스를 제거할 수 있습니다.


해당 인덱스가 다른 쿼리에서 사용중이라, 제거가 힘들겠네요 😂
ms 단위라, 성능 비교에도 큰 차이는 없을 것 같은데.. DB 부담을 덜기에도 캐싱이 적용된터라 큰 차이도 없을 것 같네요.


public List<SchoolFestivalV1Response> findPastFestivalsBySchoolId(
Long schoolId,
LocalDate today
) {
return queryDslHelper.select(
new QSchoolFestivalV1Response(
festival.id,
festival.name,
festival.startDate,
festival.endDate,
festival.thumbnail,
festivalQueryInfo.artistInfo
)
)
.from(festival)
.leftJoin(festivalQueryInfo).on(festivalQueryInfo.festivalId.eq(festival.id))
.where(festival.school.id.eq(schoolId).and(festival.endDate.lt(today)))
.stream()
.sorted(Comparator.comparing(SchoolFestivalV1Response::endDate).reversed())
.toList();
}
}
Loading
Loading