Skip to content

Commit

Permalink
refactor: lines 캐싱전략 shortestPath로 이동
Browse files Browse the repository at this point in the history
업데이트 시 캐싱 초기화 확인완료
  • Loading branch information
Livenow14 committed May 29, 2021
1 parent a9c46ef commit 2686d38
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/java/wooteco/subway/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ResponseEntity<String> databaseException(RuntimeException exception) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(exception.getMessage());
}

@ExceptionHandler(value = {MemberException.class, IllegalStateException.class })
@ExceptionHandler(value = {MemberException.class, IllegalStateException.class})
public ResponseEntity<String> badRequest(RuntimeException exception) {
logger.error(exception.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getMessage());
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/wooteco/subway/line/dao/LineDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package wooteco.subway.line.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
Expand All @@ -22,10 +19,8 @@
import java.util.stream.Collectors;

@Repository
@CacheConfig(cacheNames = {"lines"})
@CacheConfig(cacheNames = "cache::shortestPath")
public class LineDao {
private final Logger logger = LoggerFactory.getLogger(LineDao.class);

private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert insertAction;

Expand Down Expand Up @@ -70,7 +65,6 @@ public void update(Line newLine) {

@Cacheable
public List<Line> findAll() {
logger.info("캐시 들어오냐?");
String sql = "select L.id as line_id, L.name as line_name, L.color as line_color, " +
"S.id as section_id, S.distance as section_distance, " +
"UST.id as up_station_id, UST.name as up_station_name, " +
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/wooteco/subway/line/dao/SectionDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.stream.Collectors;

@Repository
@CacheConfig(cacheNames = {"lines"})
@CacheConfig(cacheNames = "cache::shortestPath")
public class SectionDao {
private final JdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert simpleJdbcInsert;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package wooteco.subway.path.application;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import wooteco.subway.line.dao.LineDao;
import wooteco.subway.line.domain.Line;
Expand All @@ -14,13 +17,17 @@

@Service
public class PathService {
private static final Logger logger = LoggerFactory.getLogger(PathService.class);

private final LineDao lineDao;

public PathService(final LineDao lineDao) {
this.lineDao = lineDao;
}

@Cacheable(value = "cache::shortestPath", key = "#sourceId.toString() + '::' + #targetId.toString()")
public PathResponse findShortestPath(final Long sourceId, final Long targetId) {
logger.info("캐싱 되에에엠");
List<Line> lines = lineDao.findAll();
Path path = new Path(new DijkstraShortestPathStrategy(), lines);
Station sourceStation = findStationById(lines, sourceId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wooteco.subway.path.domain.strategy.shortestpath;

import org.jgrapht.GraphPath;
import org.jgrapht.alg.interfaces.ShortestPathAlgorithm;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import wooteco.subway.line.domain.Line;
Expand All @@ -12,18 +11,7 @@

public class DijkstraShortestPathStrategy extends ShortestPathStrategy {
@Override
public List<Station> getVertexList(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return graphPath.getVertexList();
}

@Override
public int getWeight(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return (int) graphPath.getWeight();
}

private GraphPath<Station, DefaultWeightedEdge> graphPath(List<Line> lines, Station source, Station target) {
public GraphPath<Station, DefaultWeightedEdge> graphPath(List<Line> lines, Station source, Station target) {
DijkstraShortestPath<Station, DefaultWeightedEdge> dijkstraShortestPath = new DijkstraShortestPath<>(new PathGraph(lines).graph());
return dijkstraShortestPath.getPath(source, target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,7 @@

public class FloydWarshallShortestPathStrategy extends ShortestPathStrategy {
@Override
public List<Station> getVertexList(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return graphPath.getVertexList();
}

@Override
public int getWeight(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return (int) graphPath.getWeight();
}

private GraphPath<Station, DefaultWeightedEdge> graphPath(List<Line> lines, Station source, Station target) {
public GraphPath<Station, DefaultWeightedEdge> graphPath(List<Line> lines, Station source, Station target) {
FloydWarshallShortestPaths<Station, DefaultWeightedEdge> floydWarshallShortestPaths = new FloydWarshallShortestPaths<>(new PathGraph(lines).graph());
return floydWarshallShortestPaths.getPath(source, target);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package wooteco.subway.path.domain.strategy.shortestpath;

import org.jgrapht.GraphPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import wooteco.subway.line.domain.Line;
import wooteco.subway.station.domain.Station;

import java.util.List;

public abstract class ShortestPathStrategy {
public abstract List<Station> getVertexList(List<Line> lines, Station source, Station target);
public List<Station> getVertexList(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return graphPath.getVertexList();
}

public abstract int getWeight(List<Line> lines, Station source, Station target);
public int getWeight(List<Line> lines, Station source, Station target) {
GraphPath<Station, DefaultWeightedEdge> graphPath = graphPath(lines, source, target);
return (int) graphPath.getWeight();
}

public abstract GraphPath<Station, DefaultWeightedEdge> graphPath(List<Line> lines, Station source, Station target);
}

0 comments on commit 2686d38

Please sign in to comment.