Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 많이 조회한 게시글 키워드 구조 변경 #1061

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from

Conversation

seongjae6751
Copy link
Contributor

@seongjae6751 seongjae6751 commented Nov 20, 2024

🔥 연관 이슈

🚀 작업 내용

  1. 스케줄러 리팩토링:
  • ArticleService의 코드가 너무 방대해짐에 따라 ArticleService와 ArticleSyncService로 나눴습니다.
    • ArticleService는 비즈니스 로직 호출만 담당하고, 각 동기화 작업은 ArticleSyncService로 이동하여 독립적으로 관리하게 했습니다.
  1. 많이 검색한 키워드 로직 리팩토링:
  • 초기 코드:
    • 중복된 로직과 단일 메서드 내의 복잡한 가중치 계산으로 인해 유지보수성이 떨어졌습니다.
  • 리팩토링 후
    • RedisKeywordTracker를 도입해서 Redis의 Sorted Set과 Key-Value Store를 이용해 인기 검색 키워드의 가중치를 관리하고, 많이 검색된 키워드를 실시간으로 추적하게 했습니다.
  1. 객체지향 설계 및 Strategy 패턴 적용:
  • Strategy 패턴을 적용하여 인기 키워드 조회 시 Redis와 MySQL 병합 전략을 동적으로 선택할 수 있도록 설계했습니다.
    • Redis에 상위 키워드가 부족한 경우, MySQL에서 자동으로 보충하는 구조로 구현했습니다.

💬 리뷰 중점사항

@seongjae6751 seongjae6751 self-assigned this Nov 20, 2024
@github-actions github-actions bot added the 리팩터링 리팩터링을 위한 이슈입니다 label Nov 20, 2024
Copy link

github-actions bot commented Nov 20, 2024

Unit Test Results

339 tests   338 ✔️  1m 41s ⏱️
  45 suites      1 💤
  45 files        0

Results for commit bae19b6.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@20HyeonsuLee 20HyeonsuLee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

연관 이슈가 잘못 연결되어 있는 것 같아요~

Copy link
Contributor

@kih1015 kih1015 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

Comment on lines +15 to +45
@Component
@RequiredArgsConstructor
public class KeywordRankingManager {

private final PopularKeywordTracker popularKeywordTracker;
private final ArticleSearchKeywordRepository keywordRepository;

public List<String> getTopKeywords(int count) {
List<String> primaryKeywords = new ArrayList<>(popularKeywordTracker.getTopKeywords(count));

if (primaryKeywords.size() < count) {
int remainingCount = count - primaryKeywords.size();
List<String> secondaryKeywords = getBackupKeywords(remainingCount);
secondaryKeywords.stream()
.filter(keyword -> !primaryKeywords.contains(keyword))
.forEach(primaryKeywords::add);
}

return primaryKeywords;
}

private List<String> getBackupKeywords(int count) {
LocalDateTime fromDate = LocalDateTime.now().minusWeeks(1);
Pageable pageable = PageRequest.of(0, count);

List<String> topKeywords = keywordRepository.findTopKeywords(fromDate, pageable);
if (topKeywords.isEmpty()) {
topKeywords = keywordRepository.findTopKeywordsByLatest(pageable);
}
return topKeywords;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스 갯수가 늘어나더라도 크기를 작게 분리하는 것이 유지보수에 많은 도움이 된다고 생각합니다. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
리팩터링 리팩터링을 위한 이슈입니다
Projects
None yet
Development

Successfully merging this pull request may close these issues.

게시글 조회 많이 검색되는 키워드 api 리팩토링
3 participants