From 11fa1beed2b123830d78e20e895ed5f263f4a2e5 Mon Sep 17 00:00:00 2001 From: wooodev <142153611+wooodev@users.noreply.github.com> Date: Mon, 11 Aug 2025 22:07:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EB=B3=84=EC=A0=90=20=ED=8F=89?= =?UTF-8?q?=EA=B7=A0=20null=20=EB=A1=9C=EC=A7=81=20=EB=B3=B4=EC=99=84=20#1?= =?UTF-8?q?29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catsgotogedog/content/service/ContentSearchService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java index c5c1f60..3f552c5 100644 --- a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java +++ b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java @@ -136,11 +136,12 @@ public List search(String title, Map contentMap = contentRepository.findAllById(ids).stream() .collect(Collectors.toMap(Content::getContentId, c -> c)); + Map avgScoreMap = new HashMap<>(); List avgRows = contentReviewRepository.findAvgScoreByContentIdIn(ids); for (AvgScoreProjection row : avgRows) { Double v = row.getAvgScore(); - avgScoreMap.put(row.getContentId(),v); + avgScoreMap.put(row.getContentId(), (v != null ? v : 0.0)); } Set wishedSet; From 482bf611097bf0e948eae28baf604c56c9e08531 Mon Sep 17 00:00:00 2001 From: wooodev <142153611+wooodev@users.noreply.github.com> Date: Mon, 11 Aug 2025 22:11:43 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=A6=BC=ED=99=94=20#129?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 배치 쿼리 스트림 스타일 적용 --- .../content/service/ContentSearchService.java | 99 ++++++++----------- 1 file changed, 41 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java index 3f552c5..3ffcb2d 100644 --- a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java +++ b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java @@ -136,63 +136,46 @@ public List search(String title, Map contentMap = contentRepository.findAllById(ids).stream() .collect(Collectors.toMap(Content::getContentId, c -> c)); + Map avgScoreMap = contentReviewRepository + .findAvgScoreByContentIdIn(ids).stream() + .collect(Collectors.toMap( + AvgScoreProjection::getContentId, + p -> Optional.ofNullable(p.getAvgScore()).orElse(0.0) + )); - Map avgScoreMap = new HashMap<>(); - List avgRows = contentReviewRepository.findAvgScoreByContentIdIn(ids); - for (AvgScoreProjection row : avgRows) { - Double v = row.getAvgScore(); - avgScoreMap.put(row.getContentId(), (v != null ? v : 0.0)); - } - - Set wishedSet; - if (userId != null && !userId.isBlank()) { - wishedSet = contentWishRepository - .findWishedContentIdsByUserIdAndContentIds(Integer.parseInt(userId), ids); - } else { - wishedSet = Collections.emptySet(); - } - - Map> hashtagMap = new HashMap<>(); - List hashtags = hashtagRepository.findByContentIdIn(ids); - for (Hashtag h : hashtags) { - int cid = h.getContentId(); - List list = hashtagMap.get(cid); - if (list == null) { - list = new ArrayList<>(); - hashtagMap.put(cid, list); - } - list.add(h.getContent()); - } + Set wishedSet = (userId != null && !userId.isBlank()) + ? contentWishRepository.findWishedContentIdsByUserIdAndContentIds(Integer.parseInt(userId), ids) + : Set.of(); - Map restDateMap = new HashMap<>(); - List sightRows = sightsInformationRepository.findRestDateByContentIdIn(ids); - for (RestDateProjection r : sightRows) { - String rd = r.getRestDate(); - if (rd != null) { - restDateMap.put(r.getContentId(), rd); - } - } - List restRows = restaurantInformationRepository.findRestDateByContentIdIn(ids); - for (RestDateProjection r : restRows) { - String rd = r.getRestDate(); - if (rd != null) { - restDateMap.putIfAbsent(r.getContentId(), rd); - } - } + Map> hashtagMap = hashtagRepository.findByContentIdIn(ids).stream() + .collect(Collectors.groupingBy( + Hashtag::getContentId, + Collectors.mapping(Hashtag::getContent, Collectors.toList()) + )); - Map totalViewMap = new HashMap<>(); - List viewRows = viewTotalRepository.findTotalViewByContentIdIn(ids); - for (ViewTotalProjection v : viewRows) { - int tv = v.getTotalView(); - totalViewMap.put(v.getContentId(), tv); - } + Map restDateMap = sightsInformationRepository + .findRestDateByContentIdIn(ids).stream() + .collect(Collectors.toMap( + RestDateProjection::getContentId, + RestDateProjection::getRestDate, + (a, b) -> a + )); + restaurantInformationRepository.findRestDateByContentIdIn(ids) + .forEach(r -> restDateMap.putIfAbsent(r.getContentId(), r.getRestDate())); + + Map totalViewMap = viewTotalRepository + .findTotalViewByContentIdIn(ids).stream() + .collect(Collectors.toMap( + ViewTotalProjection::getContentId, + v -> Optional.ofNullable(v.getTotalView()).orElse(0) + )); - Map wishCntMap = new HashMap<>(); - List wishCntRows = contentWishRepository.countByContentIdIn(ids); - for (WishCountProjection w : wishCntRows) { - int cnt = w.getWishCount(); - wishCntMap.put(w.getContentId(), cnt); - } + Map wishCntMap = contentWishRepository + .countByContentIdIn(ids).stream() + .collect(Collectors.toMap( + WishCountProjection::getContentId, + w -> w.getWishCount() + )); return ids.stream() .map(contentMap::get) @@ -201,16 +184,16 @@ public List search(String title, .map(content -> { int id = content.getContentId(); - double avg = avgScoreMap.getOrDefault(id, 0.0); + double avg = Optional.ofNullable(avgScoreMap.get(id)).orElse(0.0); boolean wishData = wishedSet.contains(id); - List hashtag = hashtagMap.getOrDefault(id, List.of()); + List hashtags = hashtagMap.getOrDefault(id, List.of()); String restDate = restDateMap.get(id); - int totalView = totalViewMap.getOrDefault(id, 0); - int wishCnt = wishCntMap.getOrDefault(id, 0); + int totalView = Optional.ofNullable(totalViewMap.get(id)).orElse(0); + int wishCnt = Optional.ofNullable(wishCntMap.get(id)).orElse(0); RegionCodeResponse regionName = getRegionName(content.getSidoCode(), content.getSigunguCode()); - return ContentResponse.from(content, avg, wishData, regionName, hashtag, restDate, totalView, wishCnt); + return ContentResponse.from(content, avg, wishData, regionName, hashtags, restDate, totalView, wishCnt); }) .toList(); } From 8c93922333773473bd007e3768294338dc7284df Mon Sep 17 00:00:00 2001 From: wooodev <142153611+wooodev@users.noreply.github.com> Date: Mon, 11 Aug 2025 22:28:24 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=EC=A7=80=EC=97=AD=EB=AA=85=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 메모이제이션 적용(캐시 방식) --- .../content/service/ContentSearchService.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java index 3ffcb2d..e2ff257 100644 --- a/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java +++ b/src/main/java/com/swyp/catsgotogedog/content/service/ContentSearchService.java @@ -177,6 +177,8 @@ public List search(String title, w -> w.getWishCount() )); + Map regionCache = new HashMap<>(); + return ids.stream() .map(contentMap::get) .filter(Objects::nonNull) @@ -191,7 +193,10 @@ public List search(String title, int totalView = Optional.ofNullable(totalViewMap.get(id)).orElse(0); int wishCnt = Optional.ofNullable(wishCntMap.get(id)).orElse(0); - RegionCodeResponse regionName = getRegionName(content.getSidoCode(), content.getSigunguCode()); + String key = content.getSidoCode() + "-" + content.getSigunguCode(); + RegionCodeResponse regionName = regionCache.computeIfAbsent(key, k -> + getRegionName(content.getSidoCode(), content.getSigunguCode()) + ); return ContentResponse.from(content, avg, wishData, regionName, hashtags, restDate, totalView, wishCnt); })