Skip to content

Commit

Permalink
[UNI-255] feat: 로컬 캐시 버전 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekks committed Feb 20, 2025
1 parent 78524d9 commit 22b01d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class MapController implements MapApi {
private final MapService mapService;
private final AdminService adminService;

@GetMapping("/{univId}/routes-local")
public ResponseEntity<GetAllRoutesResDTO> getAllRoutesAndNodesByLocalCache(@PathVariable("univId") Long univId){
GetAllRoutesResDTO allRoutes = mapService.getAllRoutesByLocalCache(univId);
return ResponseEntity.ok().body(allRoutes);
}
@Override
@GetMapping("/{univId}/routes")
public ResponseEntity<GetAllRoutesResDTO> getAllRoutesAndNodes(@PathVariable("univId") Long univId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ public class MapService {

private final RedisService redisService;

private final Map<Long, List<LightRoute>> cache = new HashMap<>();

public GetAllRoutesResDTO getAllRoutesByLocalCache(Long univId) {

if(!cache.containsKey(univId)){
List<Route> routes = routeRepository.findAllRouteByUnivIdWithNodes(univId);
List<LightRoute> lightRoutes = routes.stream().map(LightRoute::new).toList();
cache.put(univId, lightRoutes);
}

List<LightRoute> routes = cache.get(univId);

// 맵이 존재하지 않을 경우 예외
if(routes.isEmpty()) {
throw new RouteException("Route Not Found", ROUTE_NOT_FOUND);
}

RevInfo revInfo = revInfoRepository.findFirstByUnivIdOrderByRevDesc(univId)
.orElseThrow(() -> new RouteException("Revision not found", RECENT_REVISION_NOT_FOUND));

AllRoutesInfo allRoutesInfo = routeCacheCalculator.assembleRoutes(routes);
GetAllRoutesResDTO response = GetAllRoutesResDTO.of(allRoutesInfo.getNodeInfos(), allRoutesInfo.getCoreRoutes(),
allRoutesInfo.getBuildingRoutes(), revInfo.getRev());

return response;
}

public GetAllRoutesResDTO getAllRoutes(Long univId) {

if(!redisService.hasData(univId.toString())){
Expand Down

0 comments on commit 22b01d0

Please sign in to comment.