Skip to content

Commit 45abc06

Browse files
committed
feat(mvc): 요청 경로 캐싱을 통한 RequestMapping 조회 성능 개선
RequestMappingRegistry에 경로 기반 캐싱(pathPatterns)을 추가하여 이미 매칭된 요청 경로에 대해 반복적인 패턴 탐색을 수행하지 않도록 최적화 - PathPattern과 HttpMethod 조합을 캐싱하여 lookup 속도 향상 - 동일한 path 요청 시 pattern matching 반복 수행 제거 - PathPattern 우선순위 비교 후 가장 구체적인 매핑을 선택하여 캐싱 - 캐시 미적중 시 기존 방식으로 fallback
1 parent 748f0d5 commit 45abc06

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/main/java/sprout/mvc/mapping/RequestMappingRegistry.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@Component
1111
public class RequestMappingRegistry {
1212
private final Map<PathPattern, Map<HttpMethod, RequestMappingInfo>> mappings = new ConcurrentHashMap<>();
13+
private final Map<String, PathPattern> pathPatterns = new ConcurrentHashMap<>();
1314

1415
public void register(PathPattern pathPattern, HttpMethod httpMethod, Object controller, Method handlerMethod) {
1516
System.out.println("Registering request mapping for " + pathPattern.getOriginalPattern() + " with http method " + httpMethod);
@@ -18,6 +19,10 @@ public void register(PathPattern pathPattern, HttpMethod httpMethod, Object cont
1819
}
1920

2021
public RequestMappingInfo getHandlerMethod(String path, HttpMethod httpMethod) {
22+
if (pathPatterns.containsKey(path)) {
23+
return mappings.get(pathPatterns.get(path)).get(httpMethod);
24+
}
25+
2126
List<RequestMappingInfo> matchingHandlers = new ArrayList<>();
2227

2328
// 1. 먼저 요청 경로와 일치하는 모든 핸들러를 찾는다.
@@ -36,6 +41,10 @@ public RequestMappingInfo getHandlerMethod(String path, HttpMethod httpMethod) {
3641
matchingHandlers.sort(Comparator.comparing(RequestMappingInfo::pattern));
3742

3843
// 3. 가장 우선순위가 높은 (가장 구체적인) 핸들러를 반환한다.
44+
45+
// 문자열 경로 캐싱
46+
pathPatterns.put(path, matchingHandlers.getFirst().pattern());
47+
3948
return matchingHandlers.get(0);
4049
}
4150
}

0 commit comments

Comments
 (0)