From 03650d3234a5fa5a329037313bf843439546b12d Mon Sep 17 00:00:00 2001 From: Photogrammer Date: Fri, 30 Aug 2024 03:27:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20StaticsCache=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/bus/service/BusStaticsService.java | 10 ++++++ .../talkka/server/bus/util/StaticsCache.java | 14 ++++++++ .../server/bus/util/StaticsCacheKey.java | 33 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 server/src/main/java/com/talkka/server/bus/util/StaticsCache.java create mode 100644 server/src/main/java/com/talkka/server/bus/util/StaticsCacheKey.java diff --git a/server/src/main/java/com/talkka/server/bus/service/BusStaticsService.java b/server/src/main/java/com/talkka/server/bus/service/BusStaticsService.java index 254283b..e376de8 100644 --- a/server/src/main/java/com/talkka/server/bus/service/BusStaticsService.java +++ b/server/src/main/java/com/talkka/server/bus/service/BusStaticsService.java @@ -13,6 +13,8 @@ import com.talkka.server.bus.dao.BusRouteStationRepository; import com.talkka.server.bus.dto.BusStaticsDto; import com.talkka.server.bus.exception.BusRouteNotFoundException; +import com.talkka.server.bus.util.StaticsCache; +import com.talkka.server.bus.util.StaticsCacheKey; import lombok.RequiredArgsConstructor; @@ -21,6 +23,7 @@ public class BusStaticsService { private final BusRemainSeatRepository busRemainSeatRepository; private final BusRouteStationRepository busRouteStationRepository; + private final StaticsCache staticsCache; /** * 버스 경로 정보를 조회합니다. @@ -43,6 +46,11 @@ public BusStaticsDto getRouteStationStatics( Long week ) { // 타겟 노선정거장 조회 + var key = new StaticsCacheKey(routeStationId, stationNum, time, timeRangeMinute, week); + if (staticsCache.get(key).isPresent()) { + return staticsCache.get(key).get(); + } + var routeStation = busRouteStationRepository.findById(routeStationId) .orElseThrow(() -> new BusRouteNotFoundException(routeStationId)); Long routeId = routeStation.getRoute().getId(); @@ -82,6 +90,8 @@ public BusStaticsDto getRouteStationStatics( ) ); } + + staticsCache.put(key, new BusStaticsDto(time, routeStationList, data)); return new BusStaticsDto( time, routeStationList, diff --git a/server/src/main/java/com/talkka/server/bus/util/StaticsCache.java b/server/src/main/java/com/talkka/server/bus/util/StaticsCache.java new file mode 100644 index 0000000..9d6caf5 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/util/StaticsCache.java @@ -0,0 +1,14 @@ +package com.talkka.server.bus.util; + +import org.springframework.stereotype.Component; + +import com.talkka.server.bus.dto.BusStaticsDto; +import com.talkka.server.common.util.MemoryCachedStorage; + +@Component +public class StaticsCache extends MemoryCachedStorage { + public StaticsCache() { + // 1일 + super(60 * 60 * 24 * 1); + } +} diff --git a/server/src/main/java/com/talkka/server/bus/util/StaticsCacheKey.java b/server/src/main/java/com/talkka/server/bus/util/StaticsCacheKey.java new file mode 100644 index 0000000..70d3baa --- /dev/null +++ b/server/src/main/java/com/talkka/server/bus/util/StaticsCacheKey.java @@ -0,0 +1,33 @@ +package com.talkka.server.bus.util; + +import java.time.LocalDateTime; + +public record StaticsCacheKey( + Long routeStationId, + Integer stationNum, + LocalDateTime time, + Integer timeRangeMinute, + Long week +) { + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + StaticsCacheKey that = (StaticsCacheKey)o; + return week.equals(that.week) && stationNum.equals(that.stationNum) && time.equals(that.time) + && routeStationId.equals(that.routeStationId) && timeRangeMinute.equals(that.timeRangeMinute); + } + + @Override + public int hashCode() { + int result = routeStationId.hashCode(); + result = 31 * result + stationNum.hashCode(); + result = 31 * result + time.hashCode(); + result = 31 * result + timeRangeMinute.hashCode(); + result = 31 * result + week.hashCode(); + return result; + } +}