-
Notifications
You must be signed in to change notification settings - Fork 1
fix: 지역 캐시 5분 revision 동기화 #683
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d040c4f
fix: refresh region cache on revision change
bottlenote-app[bot] bf12b09
fix: detect region cache content changes
bottlenote-app[bot] adde04c
fix: add transactional region cache refresh
bottlenote-app[bot] 15da78a
Merge branch 'main' into fix/region-cache-refresh
Whale0928 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
2 changes: 2 additions & 0 deletions
2
bottlenote-mono/src/main/java/app/bottlenote/alcohols/dto/response/RegionsItem.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
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
46 changes: 46 additions & 0 deletions
46
...-product-api/src/main/java/app/bottlenote/alcohols/service/RegionCacheRefreshService.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,46 @@ | ||
| package app.bottlenote.alcohols.service; | ||
|
|
||
| import app.bottlenote.alcohols.domain.RegionRepository; | ||
| import app.bottlenote.alcohols.dto.response.RegionsItem; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RegionCacheRefreshService { | ||
|
|
||
| private static final String REGION_CACHE_NAME = "local_cache_alcohol_region_information"; | ||
|
|
||
| private final RegionRepository regionRepository; | ||
| private final CacheManager cacheManager; | ||
| private List<RegionsItem> lastKnownRegions; | ||
|
|
||
| @EventListener(ApplicationReadyEvent.class) | ||
| @Transactional(readOnly = true) | ||
| public void initializeRevision() { | ||
| refresh(); | ||
| } | ||
|
|
||
| @Scheduled(cron = "${schedules.region.cache.refresh.cron:0 */5 * * * *}") | ||
| @Transactional(readOnly = true) | ||
| public synchronized void refresh() { | ||
| List<RegionsItem> currentRegions = regionRepository.findAllRegionsResponse(); | ||
| if (lastKnownRegions != null && !lastKnownRegions.equals(currentRegions)) { | ||
| Cache cache = cacheManager.getCache(REGION_CACHE_NAME); | ||
| if (cache != null) { | ||
| cache.clear(); | ||
| log.info("지역 캐시를 갱신했습니다."); | ||
| } | ||
| } | ||
| lastKnownRegions = currentRegions; | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
...duct-api/src/test/java/app/bottlenote/alcohols/service/RegionCacheRefreshServiceTest.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,79 @@ | ||
| package app.bottlenote.alcohols.service; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import app.bottlenote.alcohols.domain.Region; | ||
| import app.bottlenote.alcohols.fixture.InMemoryRegionRepository; | ||
| import java.time.LocalDateTime; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.cache.Cache; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | ||
| import org.springframework.cache.interceptor.SimpleKey; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| @Tag("unit") | ||
| @DisplayName("[unit] RegionCacheRefreshService") | ||
| class RegionCacheRefreshServiceTest { | ||
|
|
||
| private static final String REGION_CACHE_NAME = "local_cache_alcohol_region_information"; | ||
|
|
||
| private final InMemoryRegionRepository regionRepository = new InMemoryRegionRepository(); | ||
| private final CacheManager cacheManager = new ConcurrentMapCacheManager(REGION_CACHE_NAME); | ||
| private RegionCacheRefreshService service; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| service = new RegionCacheRefreshService(regionRepository, cacheManager); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("지역이 추가되면 Product 지역 캐시를 비운다") | ||
| void refresh_whenRegionAdded_clearsRegionCache() { | ||
| service.refresh(); | ||
|
|
||
| Cache cache = cacheManager.getCache(REGION_CACHE_NAME); | ||
| cache.put(SimpleKey.EMPTY, "cached-regions"); | ||
| regionRepository.save(Region.builder().korName("스코틀랜드").engName("Scotland").build()); | ||
|
|
||
| service.refresh(); | ||
|
|
||
| assertThat(cache.get(SimpleKey.EMPTY)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("같은 최종 수정 시각에 지역 내용이 변경되면 Product 지역 캐시를 비운다") | ||
| void refresh_whenRegionContentChangedWithSameLastModifyAt_clearsRegionCache() { | ||
| Region region = Region.builder().korName("스코틀랜드").engName("Scotland").build(); | ||
| regionRepository.save(region); | ||
| ReflectionTestUtils.setField(region, "lastModifyAt", LocalDateTime.of(2026, 7, 29, 12, 5)); | ||
| service.refresh(); | ||
|
|
||
| Cache cache = cacheManager.getCache(REGION_CACHE_NAME); | ||
| cache.put(SimpleKey.EMPTY, "cached-regions"); | ||
| region.update("스코틀랜드", "Scotland", null, "변경된 설명", null, null, null); | ||
|
|
||
| service.refresh(); | ||
|
|
||
| assertThat(cache.get(SimpleKey.EMPTY)).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("지역이 삭제되면 Product 지역 캐시를 비운다") | ||
| void refresh_whenRegionDeleted_clearsRegionCache() { | ||
| Region region = Region.builder().korName("스코틀랜드").engName("Scotland").build(); | ||
| regionRepository.save(region); | ||
| service.refresh(); | ||
|
|
||
| Cache cache = cacheManager.getCache(REGION_CACHE_NAME); | ||
| cache.put(SimpleKey.EMPTY, "cached-regions"); | ||
| regionRepository.delete(region); | ||
|
|
||
| service.refresh(); | ||
|
|
||
| assertThat(cache.get(SimpleKey.EMPTY)).isNull(); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Blocking — rule-tests 실패
@Service의 public 메서드는@Transactional을 가져야 한다는 ArchUnit 규칙 때문에initializeRevision()과refresh()가 실패합니다. DB 조회 경계인refresh()에는@Transactional(readOnly = true)를 적용하고,initializeRevision()의 public 메서드 규칙도 프로젝트 방식에 맞게 충족시켜 주세요. 현재 CIrule-tests는 이 위반 2건으로 실패 중입니다.