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"); } }