From d13d4e5f24ae6a30094bb8cdfd8443a86eda64b1 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Fri, 23 Aug 2024 12:14:17 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=8B=A4=EC=8B=9C=EA=B0=84=20=EC=9D=91?= =?UTF-8?q?=EB=B3=B4=20=EB=B9=84=EC=9C=A8=20=EC=A1=B0=ED=9A=8C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=EC=97=90=EC=84=9C=20=ED=95=B4=EB=8B=B9=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=EA=B0=80=20=EC=84=A0=ED=83=9D=ED=95=9C=20=EC=98=B5?= =?UTF-8?q?=EC=85=98=20=EC=A1=B0=ED=9A=8C=20=EB=B6=80=EB=B6=84=EC=97=90=20?= =?UTF-8?q?=EB=A1=9C=EC=BB=AC=20=EC=BA=90=EC=8B=B1=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../eventService/EventCacheService.java | 25 +++++++++++++++++++ .../eventService/RushEventService.java | 15 +++++------ .../global/config/CacheConfig.java | 2 +- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/EventCacheService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/EventCacheService.java index 6bc1698d..1c3bb653 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/EventCacheService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/EventCacheService.java @@ -6,24 +6,30 @@ import JGS.CasperEvent.domain.event.entity.event.RushEvent; import JGS.CasperEvent.domain.event.repository.eventRepository.LotteryEventRepository; import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository; +import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository; import JGS.CasperEvent.global.enums.CustomErrorCode; import JGS.CasperEvent.global.error.exception.CustomException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.time.LocalDate; import java.util.List; +import java.util.Optional; @Service @RequiredArgsConstructor @Slf4j public class EventCacheService { + private final CacheManager cacheManager; private final RushEventRepository rushEventRepository; private final LotteryEventRepository lotteryEventRepository; + private final RushParticipantsRepository rushParticipantsRepository; @Cacheable(value = "ongoingLotteryEvent") public LotteryEvent getLotteryEvent(){ @@ -102,4 +108,23 @@ private List fetchAllRushEvent() { .map(MainRushEventResponseDto::of) .toList(); } + + // phoneNumber와 date 따른 optionId 캐싱 + @Cacheable(value = "userOptionCache", key = "#today + ':' + #phoneNumber") + public int getOptionId(LocalDate today, String phoneNumber) { + return fetchOptionId(phoneNumber); + } + + // userOptionCache 전체 초기화 + public void clearUserOptionCache() { + Cache userOptionCache = cacheManager.getCache("userOptionCache"); + if (userOptionCache != null) { + userOptionCache.clear(); + } + } + + private int fetchOptionId(String phoneNumber) { + Optional optionId = rushParticipantsRepository.getOptionIdByUserId(phoneNumber); + return optionId.orElseThrow(() -> new CustomException("유저가 응모한 선택지가 존재하지 않습니다.", CustomErrorCode.USER_NOT_FOUND)); + } } diff --git a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java index 51d0bcdc..dc061c71 100644 --- a/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java +++ b/Server/src/main/java/JGS/CasperEvent/domain/event/service/eventService/RushEventService.java @@ -96,17 +96,15 @@ public void apply(BaseUser user, int optionId) { public RushEventRateResponseDto getRushEventRate(BaseUser user) { LocalDate today = LocalDate.now(); Long todayEventId = eventCacheService.getTodayEvent(today).rushEventId(); - Optional optionId = rushParticipantsRepository.getOptionIdByUserId(user.getPhoneNumber()); + + // 해당 유저의 optionId 를 가져옴 + int optionId = eventCacheService.getOptionId(today, user.getPhoneNumber()); long leftOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 1); long rightOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 2); - // redis 에 캐싱 값 가져옴 -// long leftOptionCount = rushEventRedisService.getOptionCount(todayEventId, 1); -// long rightOptionCount = rushEventRedisService.getOptionCount(todayEventId, 2); - return new RushEventRateResponseDto( - optionId.orElseThrow(() -> new CustomException("유저가 응모한 선택지가 존재하지 않습니다.", CustomErrorCode.USER_NOT_FOUND)), + optionId, leftOptionCount, rightOptionCount); } @@ -234,8 +232,11 @@ public void setRushEvents() { rushEvents.add(rushEvent); } - eventCacheService.setCacheValue(LocalDate.now()); + LocalDate today = LocalDate.now(); + + eventCacheService.setCacheValue(today); eventCacheService.setAllRushEvent(); + eventCacheService.clearUserOptionCache(); rushEventRedisService.clearAllrushEventRate(); } diff --git a/Server/src/main/java/JGS/CasperEvent/global/config/CacheConfig.java b/Server/src/main/java/JGS/CasperEvent/global/config/CacheConfig.java index f15f5dac..7eed87ab 100644 --- a/Server/src/main/java/JGS/CasperEvent/global/config/CacheConfig.java +++ b/Server/src/main/java/JGS/CasperEvent/global/config/CacheConfig.java @@ -10,7 +10,7 @@ public class CacheConfig { @Bean public CacheManager cacheManager() { - return new ConcurrentMapCacheManager("todayRushEventCache", "ongoingLotteryEvent", "allRushEventCache"); + return new ConcurrentMapCacheManager("todayRushEventCache", "ongoingLotteryEvent", "allRushEventCache", "userOptionCache"); } }