Skip to content

Commit

Permalink
Merge pull request #198 from softeerbootcamp4th/feature/#194-local-cache
Browse files Browse the repository at this point in the history
fix: 테스트 API 호출 시 @cACHEpUT 으로 무조건 캐시 갱신되도록 변경
  • Loading branch information
wjddn2165 authored Aug 21, 2024
2 parents e13fa11 + 70add11 commit 9c9f82b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
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;

Expand All @@ -21,11 +20,21 @@
public class RushEventCacheService {

private final RushEventRepository rushEventRepository;
private final CacheManager cacheManager;

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

@CachePut(value = "todayEventCache", key = "#today")
public RushEventResponseDto setCacheValue(LocalDate today) {
log.info("이벤트 업데이트 {}", today);
return fetchTodayRushEvent(today);
}

private RushEventResponseDto fetchTodayRushEvent(LocalDate today) {
// 오늘 날짜에 해당하는 모든 이벤트 꺼내옴
List<RushEvent> rushEventList = rushEventRepository.findByEventDate(today);

Expand All @@ -39,11 +48,4 @@ public RushEventResponseDto getTodayEvent(LocalDate today) {

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

public void setCacheValue(LocalDate today, RushEventResponseDto rushEvent) {
Cache cache = cacheManager.getCache("todayEventCache");
if (cache != null) {
cache.put(today, rushEvent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void setRushEvents() {
rushEvents.add(rushEvent);
}

rushEventCacheService.setCacheValue(LocalDate.now(), RushEventResponseDto.of(rushEvents.get(2)));
rushEventCacheService.setCacheValue(LocalDate.now());
}


Expand Down

0 comments on commit 9c9f82b

Please sign in to comment.