Skip to content

Commit

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

feat: 전체 rushEvent 조회 로컬 캐시 적용
  • Loading branch information
wjddn2165 authored Aug 22, 2024
2 parents 3fcb7ee + 3aa8c5d commit 9a37d85
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package JGS.CasperEvent.domain.event.service.eventService;

import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.MainRushEventResponseDto;
import JGS.CasperEvent.domain.event.dto.ResponseDto.rushEventResponseDto.RushEventResponseDto;
import JGS.CasperEvent.domain.event.entity.event.LotteryEvent;
import JGS.CasperEvent.domain.event.entity.event.RushEvent;
Expand Down Expand Up @@ -50,14 +51,14 @@ private LotteryEvent fetchOngoingLotteryEvent() {
return lotteryEventList.get(0);
}

@Cacheable(value = "todayEventCache", key = "#today")
@Cacheable(value = "todayRushEventCache", key = "#today")
public RushEventResponseDto getTodayEvent(LocalDate today) {
log.info("오늘의 이벤트 캐싱 {}", today);
// 오늘 날짜에 해당하는 모든 이벤트 꺼내옴
return fetchTodayRushEvent(today);
}

@CachePut(value = "todayEventCache", key = "#today")
@CachePut(value = "todayRushEventCache", key = "#today")
public RushEventResponseDto setCacheValue(LocalDate today) {
log.info("이벤트 업데이트 {}", today);
return fetchTodayRushEvent(today);
Expand All @@ -77,4 +78,28 @@ private RushEventResponseDto fetchTodayRushEvent(LocalDate today) {

return RushEventResponseDto.of(rushEventList.get(0));
}

@Cacheable(value = "allRushEventCache")
public List<MainRushEventResponseDto> getAllRushEvent() {
log.info("전체 이벤트 캐싱");
// 오늘 날짜에 해당하는 모든 이벤트 꺼내옴
return fetchAllRushEvent();
}

//todo: 어드민 선착순 이벤트 변경 시 메서드 호출
@CachePut(value = "allRushEventCache")
public List<MainRushEventResponseDto> setAllRushEvent() {
log.info("이벤트 변경 캐싱");
return fetchAllRushEvent();
}

private List<MainRushEventResponseDto> fetchAllRushEvent() {
// DB에서 모든 RushEvent 가져오기
List<RushEvent> rushEventList = rushEventRepository.findAll();

// RushEvent를 DTO로 전환
return rushEventList.stream()
.map(MainRushEventResponseDto::of)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.util.RepositoryErrorHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -38,23 +39,18 @@ public RushEventListResponseDto getAllRushEvents() {
// 오늘의 선착순 이벤트 꺼내오기
RushEventResponseDto todayEvent = eventCacheService.getTodayEvent(today);

// DB에서 모든 RushEvent 가져오기
List<RushEvent> rushEventList = rushEventRepository.findAll();
// 모든 이벤트 꺼내오기
List<MainRushEventResponseDto> mainRushEventDtoList = eventCacheService.getAllRushEvent();

// 선착순 이벤트 전체 시작 날짜와 종료 날짜 구하기
List<LocalDate> dates = rushEventList.stream().map(rushEvent -> rushEvent.getStartDateTime().toLocalDate()).sorted().toList();
List<LocalDate> dates = mainRushEventDtoList.stream().map(rushEvent -> rushEvent.getStartDateTime().toLocalDate()).sorted().toList();

LocalDate totalStartDate = dates.get(0);
LocalDate totalEndDate = dates.get(dates.size() - 1);

// 전체 이벤트 기간 구하기
long activePeriod = totalStartDate.until(totalEndDate).getDays() + 1;

// RushEvent를 DTO로 전환
List<MainRushEventResponseDto> mainRushEventDtoList = rushEventList.stream()
.map(MainRushEventResponseDto::of)
.toList();

// DTO 리스트와 서버 시간을 담은 RushEventListAndServerTimeResponse 객체 생성 후 반환
return new RushEventListResponseDto(
mainRushEventDtoList,
Expand All @@ -73,6 +69,7 @@ public boolean isExists(String userId) {
return rushParticipantsRepository.existsByRushEvent_RushEventIdAndBaseUser_PhoneNumber(todayEventId, userId);
}


@Transactional
public void apply(BaseUser user, int optionId) {
LocalDate today = LocalDate.now();
Expand Down Expand Up @@ -229,6 +226,7 @@ public void setRushEvents() {
}

eventCacheService.setCacheValue(LocalDate.now());
eventCacheService.setAllRushEvent();
}


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("todayEventCache", "ongoingLotteryEvent");
return new ConcurrentMapCacheManager("todayRushEventCache", "ongoingLotteryEvent", "allRushEventCache");
}

}

0 comments on commit 9a37d85

Please sign in to comment.