Skip to content

Commit

Permalink
Merge pull request #227 from softeerbootcamp4th/feature/#203-rush-eve…
Browse files Browse the repository at this point in the history
…nt-apply-stress-test

fix: 실시간 응보 비율 조회 로직에서 해당 유저가 선택한 옵션 조회 부분에 로컬 캐싱 적용
  • Loading branch information
k000927 authored Aug 23, 2024
2 parents 7f8522c + d13d4e5 commit 63f210a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -102,4 +108,23 @@ private List<MainRushEventResponseDto> 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<Integer> optionId = rushParticipantsRepository.getOptionIdByUserId(phoneNumber);
return optionId.orElseThrow(() -> new CustomException("유저가 응모한 선택지가 존재하지 않습니다.", CustomErrorCode.USER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,15 @@ public void apply(BaseUser user, int optionId) {
public RushEventRateResponseDto getRushEventRate(BaseUser user) {
LocalDate today = LocalDate.now();
Long todayEventId = eventCacheService.getTodayEvent(today).rushEventId();
Optional<Integer> 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);
}

Expand Down Expand Up @@ -235,8 +233,11 @@ public void setRushEvents() {
rushEvents.add(rushEvent);
}

eventCacheService.setCacheValue(LocalDate.now());
LocalDate today = LocalDate.now();

eventCacheService.setCacheValue(today);
eventCacheService.setAllRushEvent();
eventCacheService.clearUserOptionCache();
rushEventRedisService.clearAllrushEventRate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CacheConfig {

@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("todayRushEventCache", "ongoingLotteryEvent", "allRushEventCache");
return new ConcurrentMapCacheManager("todayRushEventCache", "ongoingLotteryEvent", "allRushEventCache", "userOptionCache");
}

}

0 comments on commit 63f210a

Please sign in to comment.