Skip to content

Commit

Permalink
Merge pull request #154 from BOOK-TALK/#153-enhance-recommend-api
Browse files Browse the repository at this point in the history
[Enhancement] #153 책 추천 API 응답 속도 개선
  • Loading branch information
chanwoo7 authored Sep 19, 2024
2 parents 927b989 + 6a6b720 commit 389a851
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public ResponseEntity<?> recommend(@RequestParam String isbn) throws Exception {

HashSet<String> duplicateCheckSet = new HashSet<>();
LinkedList<RecommendListResponseDto> duplicateRemovedList = bookService.duplicateChecker(response, duplicateCheckSet);
bookService.ensureRecommendationsCount(duplicateRemovedList, duplicateCheckSet);
response = RandomPicker.randomPick(duplicateRemovedList, 10); // 10개 랜덤 추출

return responseTemplate.success(response, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ public LinkedList<RecommendListResponseDto> recommend(RecommendListRequestDto re
return responseList;
}

/* 추천 책 수가 10보다 작으면 추천된 isbn 으로 recommend() 다시 호출 */
public void ensureRecommendationsCount(LinkedList<RecommendListResponseDto> list, HashSet<String> set) throws Exception {
log.trace("BookService > ensureRecommendationsCount()");
LinkedList<RecommendListResponseDto> originalList = new LinkedList<>(list);
Iterator<RecommendListResponseDto> iterator = originalList.iterator();
while (originalList.size() < 10 && iterator.hasNext()) {
RecommendListRequestDto newRequestDto = RecommendListRequestDto.builder().isbn13(iterator.next().getIsbn13()).build(); // 추천된 다른 책의 isbn13
LinkedList<RecommendListResponseDto> newRecommendList = recommend(newRequestDto);
// 기존에 추가된 책인지 확인
for(RecommendListResponseDto dto : newRecommendList){
String key = dto.getBookname() + dto.getAuthors();
if(set.add(key)) list.add(dto);
}
}
}

public LinkedList<RecommendListResponseDto> duplicateChecker(LinkedList<RecommendListResponseDto> list, HashSet<String> set){
log.trace("BookService > duplicateChecker()");
LinkedList<RecommendListResponseDto> duplicateRemovedList = new LinkedList<>();
Expand Down

0 comments on commit 389a851

Please sign in to comment.