Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] StaticsCache 추가 #163

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,6 +23,7 @@
public class BusStaticsService {
private final BusRemainSeatRepository busRemainSeatRepository;
private final BusRouteStationRepository busRouteStationRepository;
private final StaticsCache staticsCache;

/**
* 버스 경로 정보를 조회합니다.
Expand All @@ -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();
Expand Down Expand Up @@ -82,6 +90,8 @@ public BusStaticsDto getRouteStationStatics(
)
);
}

staticsCache.put(key, new BusStaticsDto(time, routeStationList, data));
return new BusStaticsDto(
time,
routeStationList,
Expand Down
14 changes: 14 additions & 0 deletions server/src/main/java/com/talkka/server/bus/util/StaticsCache.java
Original file line number Diff line number Diff line change
@@ -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<StaticsCacheKey, BusStaticsDto> {
public StaticsCache() {
// 1일
super(60 * 60 * 24 * 1);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading