-
Notifications
You must be signed in to change notification settings - Fork 1
[Feature] /questions/today 집계 캐시 적용 및 캐시 스탬피드 방지 #70
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ifu/ifu_server/global/config/CacheWarmupRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.ifu.ifu_server.global.config; | ||
|
|
||
| import com.ifu.ifu_server.domain.question.service.TodayQuestionCacheService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.ApplicationArguments; | ||
| import org.springframework.boot.ApplicationRunner; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * 애플리케이션 시작 시 캐시 pre-warm | ||
| * - 캐시 스탬피드 방지: 서버 구동 직후 캐시가 비어있는 상태에서 | ||
| * 동시 요청으로 인한 DB 폭주를 막기 위해 미리 캐시를 채움 | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class CacheWarmupRunner implements ApplicationRunner { | ||
|
|
||
| private final TodayQuestionCacheService cacheService; | ||
|
|
||
| @Override | ||
| public void run(ApplicationArguments args) { | ||
| try { | ||
| cacheService.getTodayQuestionBase(); | ||
| log.info("[Startup] 캐시 pre-warm 완료"); | ||
| } catch (Exception e) { | ||
| log.warn("[Startup] 캐시 pre-warm 실패: {}", e.getMessage()); | ||
| } | ||
qkrcodus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
48 changes: 47 additions & 1 deletion
48
src/main/java/com/ifu/ifu_server/global/config/LocalCacheConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,70 @@ | ||
| package com.ifu.ifu_server.global.config; | ||
|
|
||
| import java.util.Objects; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
|
|
||
| /** | ||
| * 로컬 캐시 설정 | ||
| * - Spring Cache Abstraction 사용 | ||
| * - ConcurrentMapCacheManager로 로컬 캐시 구현 | ||
| * - 집계 데이터 캐시는 스케줄러로 주기적 evict (TTL 대체) | ||
| */ | ||
| @Slf4j | ||
| @EnableCaching | ||
| @EnableScheduling | ||
| @Configuration | ||
| public class LocalCacheConfig { | ||
|
|
||
| public static final String TODAY_QUESTION_CACHE = "todayQuestion"; | ||
| public static final String QUESTION_VOTE_STATS_CACHE = "questionVoteStats"; | ||
| public static final String QUESTION_COMMENT_COUNT_CACHE = "questionCommentCount"; | ||
|
|
||
| private final CacheManager cacheManager; | ||
|
|
||
| public LocalCacheConfig() { | ||
| this.cacheManager = new ConcurrentMapCacheManager( | ||
| TODAY_QUESTION_CACHE, | ||
| QUESTION_VOTE_STATS_CACHE, | ||
| QUESTION_COMMENT_COUNT_CACHE | ||
| ); | ||
| } | ||
|
|
||
| @Bean | ||
| public CacheManager cacheManager() { | ||
| return new ConcurrentMapCacheManager(TODAY_QUESTION_CACHE); | ||
| return cacheManager; | ||
| } | ||
|
|
||
| /** | ||
| * VoteStats 캐시 주기적 evict (5초마다) | ||
| * - ConcurrentMapCacheManager는 TTL 미지원이므로 스케줄러로 대체 | ||
| */ | ||
| @Scheduled(fixedRate = 5000) | ||
| public void evictVoteStatsCache() { | ||
| Cache cache = cacheManager.getCache(QUESTION_VOTE_STATS_CACHE); | ||
| if (cache != null) { | ||
| cache.clear(); | ||
| log.debug("[Cache Evict] VoteStats 캐시 만료 - 5초 TTL"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * CommentCount 캐시 주기적 evict (10초마다) | ||
| * - ConcurrentMapCacheManager는 TTL 미지원이므로 스케줄러로 대체 | ||
| */ | ||
| @Scheduled(fixedRate = 10000) | ||
| public void evictCommentCountCache() { | ||
| Cache cache = cacheManager.getCache(QUESTION_COMMENT_COUNT_CACHE); | ||
| if (cache != null) { | ||
| cache.clear(); | ||
| log.debug("[Cache Evict] CommentCount 캐시 만료 - 10초 TTL"); | ||
| } | ||
| } | ||
qkrcodus marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.