Skip to content

Commit cd84ec5

Browse files
authored
Merge pull request #45 from team8-nbc/feat/search
[Refactor] ๋ถˆํ•„์š”ํ•œ ์บ์‹œ ๊ด€๋ จ DB ์ œ๊ฑฐ
2 parents 642fa2c + 4da6c78 commit cd84ec5

13 files changed

Lines changed: 33 additions & 180 deletions

File tree

โ€Žsrc/main/java/com/example/eightyage/domain/product/service/ProductService.javaโ€Ž

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Page<ProductSearchResponseDto> getProductsV1(String productName, Category
103103
Page<ProductSearchResponseDto> productsResponse = productRepository.findProductsOrderByReviewScore(productName, category, pageable);
104104

105105
if (StringUtils.hasText(productName) && !productsResponse.isEmpty()) {
106-
searchServiceV1.saveSearchLog(productName); // ๋กœ๊ทธ๋งŒ ์ €์žฅ
106+
searchServiceV1.saveSearchLog(productName); // ๋กœ๊ทธ ์ €์žฅ
107107
}
108108
return productsResponse;
109109
}
@@ -117,7 +117,6 @@ public Page<ProductSearchResponseDto> getProductsV2(String productName, Category
117117

118118
if (StringUtils.hasText(productName) && !productsResponse.isEmpty()) {
119119
searchServiceV2.saveSearchLog(productName); // ๋กœ๊ทธ ์ €์žฅ
120-
searchServiceV2.increaseKeywordCount(productName); // ์บ์‹œ ์ž‘์—…
121120
}
122121

123122
return productsResponse;
@@ -131,8 +130,8 @@ public Page<ProductSearchResponseDto> getProductsV3(String productName, Category
131130
Page<ProductSearchResponseDto> productsResponse = productRepository.findProductsOrderByReviewScore(productName, category, pageable);
132131

133132
if(StringUtils.hasText(productName) && !productsResponse.isEmpty()){
134-
searchServiceV3.saveSearchLog(productName);
135-
searchServiceV3.increaseSortedKeywordRank(productName);
133+
searchServiceV3.saveSearchLog(productName); // ๋กœ๊ทธ ์ €์žฅ
134+
searchServiceV3.increaseSortedKeywordRank(productName); // ์บ์‹œ ์ถ”๊ฐ€
136135
}
137136
return productsResponse;
138137
}
@@ -154,5 +153,4 @@ public Product findProductByIdOrElseThrow(Long productId) {
154153
);
155154
}
156155

157-
158156
}

โ€Žsrc/main/java/com/example/eightyage/domain/search/dto/PopularKeywordDto.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PopularKeywordDto {
1414
private String keyword;
1515
private Long count;
1616

17-
public static PopularKeywordDto of(String keyword, Long score) {
17+
public static PopularKeywordDto keywordOf(String keyword, Long score) {
1818
return new PopularKeywordDto(keyword, score);
1919
}
2020
}

โ€Žsrc/main/java/com/example/eightyage/domain/search/entity/KeywordCount.javaโ€Ž

Lines changed: 0 additions & 25 deletions
This file was deleted.

โ€Žsrc/main/java/com/example/eightyage/domain/search/entity/SearchLog.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SearchLog {
2727
@CreatedDate
2828
private LocalDateTime searchedAt;
2929

30-
public static SearchLog of(String keyword) {
30+
public static SearchLog keywordOf(String keyword) {
3131
SearchLog log = new SearchLog();
3232
log.keyword = keyword;
3333
return log;

โ€Žsrc/main/java/com/example/eightyage/domain/search/repository/KeywordCountRepository.javaโ€Ž

Lines changed: 0 additions & 8 deletions
This file was deleted.

โ€Žsrc/main/java/com/example/eightyage/domain/search/service/v1/SearchServiceV1.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SearchServiceV1 {
1818
@Transactional(propagation = Propagation.REQUIRES_NEW)
1919
public void saveSearchLog(String keyword){
2020
if(StringUtils.hasText(keyword)){
21-
searchLogRepository.save(SearchLog.of(keyword));
21+
searchLogRepository.save(SearchLog.keywordOf(keyword));
2222
}
2323
}
2424
}

โ€Žsrc/main/java/com/example/eightyage/domain/search/service/v2/KeywordCountFlushService.javaโ€Ž

Lines changed: 0 additions & 73 deletions
This file was deleted.

โ€Žsrc/main/java/com/example/eightyage/domain/search/service/v2/PopularKeywordServiceV2.javaโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public class PopularKeywordServiceV2 implements PopularKeywordService {
1919
private final SearchLogRepository searchLogRepository;
2020
private static final int MIN_DAYS = 1;
2121
private static final int MAX_DAYS = 365;
22-
22+
private static final String POPULAR_KEYWORDS = "popularKeywords";
2323

2424
//์บ์‹œO ์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด ์กฐํšŒ
2525
@Transactional(readOnly = true)
26-
@Cacheable(value = "popularKeywords", key = "#days")
26+
@Cacheable(value = POPULAR_KEYWORDS, key = "#days")
2727
public List<PopularKeywordDto> searchPopularKeywords(int days) {
2828
if (days < MIN_DAYS || days > MAX_DAYS) {
2929
throw new BadRequestException("์กฐํšŒ ์ผ ์ˆ˜๋Š” 1~365 ์‚ฌ์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.");

โ€Žsrc/main/java/com/example/eightyage/domain/search/service/v2/SearchServiceV2.javaโ€Ž

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,23 @@
33
import com.example.eightyage.domain.search.entity.SearchLog;
44
import com.example.eightyage.domain.search.repository.SearchLogRepository;
55
import lombok.RequiredArgsConstructor;
6-
import org.springframework.cache.Cache;
7-
import org.springframework.cache.CacheManager;
86
import org.springframework.stereotype.Service;
97
import org.springframework.transaction.annotation.Propagation;
108
import org.springframework.transaction.annotation.Transactional;
119
import org.springframework.util.StringUtils;
1210

13-
import java.util.HashSet;
14-
import java.util.Set;
1511

1612
@Service
1713
@RequiredArgsConstructor
1814
public class SearchServiceV2 {
1915

2016
private final SearchLogRepository searchLogRepository;
21-
private final CacheManager cacheManager;
22-
private static final String KEYWORD_COUNT_MAP = "keywordCountMap";
23-
private static final String KEYWORD_KEY_SET = "keywordKeySet";
24-
2517

2618
// ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋ฅผ ๋กœ๊ทธ์— ์ €์žฅ
2719
@Transactional(propagation = Propagation.REQUIRES_NEW)
2820
public void saveSearchLog(String keyword) {
2921
if (StringUtils.hasText(keyword)) {
30-
searchLogRepository.save(SearchLog.of(keyword));
31-
}
32-
}
33-
34-
// ๊ฒ€์ƒ‰ ์‹œ ํ‚ค์›Œ๋“œ ์นด์šดํŠธ ์ฆ๊ฐ€
35-
@Transactional
36-
public void increaseKeywordCount(String keyword) {
37-
if (!StringUtils.hasText(keyword)) return;
38-
39-
Cache countCache = cacheManager.getCache(KEYWORD_COUNT_MAP);
40-
41-
if (countCache != null) {
42-
String countStr = countCache.get(keyword, String.class);
43-
long count = (countStr == null) ? 1L : Long.parseLong(countStr) + 1;
44-
countCache.put(keyword, Long.toString(count));
45-
}
46-
47-
updateKeywordSet(keyword);
48-
}
49-
50-
// ์บ์‹œ์— ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€
51-
private void updateKeywordSet(String keyword) {
52-
Cache keySetCache = cacheManager.getCache(KEYWORD_KEY_SET);
53-
if (keySetCache != null) {
54-
Set<String> keywordSet = keySetCache.get("keywords", Set.class);
55-
if (keywordSet == null) {
56-
keywordSet = new HashSet<>();
57-
}
58-
keywordSet.add(keyword);
59-
keySetCache.put("keywords", keywordSet);
22+
searchLogRepository.save(SearchLog.keywordOf(keyword));
6023
}
6124
}
6225
}

โ€Žsrc/main/java/com/example/eightyage/domain/search/service/v3/PopularKeywordServiceV3.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public List<PopularKeywordDto> searchPopularKeywords(int limit) {
2828
if (keywordSet == null) {
2929
return List.of();
3030
}
31-
return keywordSet.stream().map(tuple -> PopularKeywordDto.of(tuple.getValue(), Objects.requireNonNull(tuple.getScore()).longValue()))
31+
return keywordSet.stream().map(tuple -> PopularKeywordDto.keywordOf(tuple.getValue(), Objects.requireNonNull(tuple.getScore()).longValue()))
3232
.collect(Collectors.toList());
3333
}
3434
}

0 commit comments

Comments
ย (0)