Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/com/catchtable/store/entity/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
@Table(
name = "stores",
indexes = {
@Index(name = "idx_store_lat_lng", columnList = "latitude, longitude"),
@Index(name = "idx_store_deleted_category", columnList = "is_deleted, category"),
@Index(name = "idx_store_deleted_district", columnList = "is_deleted, district")
@Index(name = "idx_store_lat_lng", columnList = "latitude, longitude"),
@Index(name = "idx_store_deleted_category", columnList = "is_deleted, category"),
@Index(name = "idx_store_deleted_district", columnList = "is_deleted, district"),
@Index(name = "idx_store_popularity", columnList = "is_deleted, average_star DESC, review_count DESC, bookmark_count DESC, id ASC")
}
)
@Getter
Expand Down
21 changes: 6 additions & 15 deletions src/main/java/com/catchtable/store/repository/StoreRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ public interface StoreRepository extends JpaRepository<Store, Long>, JpaSpecific

List<Store> findAllByIsDeletedFalse();

/**
* ์ธ๊ธฐ ๋งค์žฅ ์ •๋ ฌ โ€” averageStar DESC โ†’ reviewCount DESC โ†’ bookmarkCount DESC โ†’ id ASC
* - averageStar๋Š” NULL์ผ ์ˆ˜ ์žˆ์–ด COALESCE๋กœ 0 ์ฒ˜๋ฆฌ
* - ๋ชจ๋“  ํ†ต๊ณ„ ๊ฐ’์ด ๋™๋ฅ ์ผ ๋•Œ ID ์˜ค๋ฆ„์ฐจ์ˆœ(=๋จผ์ € ๋“ฑ๋ก๋œ ์ˆœ)์œผ๋กœ ๊ฒฐ์ •์„ฑ ๋ณด์žฅ
*/
@Query(value = """
SELECT s FROM Store s
WHERE s.isDeleted = false
ORDER BY COALESCE(s.averageStar, 0) DESC,
ORDER BY s.averageStar DESC,
s.reviewCount DESC,
s.bookmarkCount DESC,
s.id ASC
Expand Down Expand Up @@ -80,9 +75,9 @@ AND ST_DistanceSphere(
ST_MakePoint(s.longitude, s.latitude),
ST_MakePoint(:lon, :lat)
) <= :radiusMeters
ORDER BY COALESCE(s.average_star, 0) DESC,
COALESCE(s.review_count, 0) DESC,
COALESCE(s.bookmark_count, 0) DESC,
ORDER BY s.average_star DESC,
s.review_count DESC,
s.bookmark_count DESC,
s.id ASC
""",
nativeQuery = true)
Expand Down Expand Up @@ -121,12 +116,8 @@ List<Store> findNearbyWithGist(@Param("lat") double latitude,
@Query(value = """
SELECT * FROM stores s
WHERE s.is_deleted = false
AND s.latitude BETWEEN :minLat AND :maxLat
AND s.longitude BETWEEN :minLng AND :maxLng
ORDER BY ST_DistanceSphere(
ST_MakePoint(s.longitude, s.latitude),
ST_MakePoint(:centerLng, :centerLat)
) ASC,
AND s.location && ST_MakeEnvelope(:minLng, :minLat, :maxLng, :maxLat, 4326)::geography
ORDER BY s.location <-> ST_SetSRID(ST_MakePoint(:centerLng, :centerLat), 4326)::geography ASC,
s.id ASC
""",
nativeQuery = true)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/catchtable/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class StoreService {
private final StoreRemainRepository storeRemainRepository;

// ๋งค์žฅ ๋“ฑ๋ก
@CacheEvict(value = "storeList", allEntries = true)
@Transactional
public StoreCreateResponse createStore(Long userId, StoreCreateRequest request) {
userRepository.getAdminOrThrow(userId, ErrorCode.ADMIN_ONLY_STORE_CREATE);
Expand All @@ -57,6 +58,7 @@ public StoreCreateResponse createStore(Long userId, StoreCreateRequest request)
* ๋งค์žฅ ๋ชฉ๋ก ํ†ตํ•ฉ ์กฐํšŒ (์ด๋ฆ„ยท์นดํ…Œ๊ณ ๋ฆฌยท์ง€์—ญ ์˜ต์…”๋„ ํ•„ํ„ฐ + DB ํŽ˜์ด์ง€๋„ค์ด์…˜ + ์ธ๊ธฐ ์ •๋ ฌ)
* Specification ์‚ฌ์šฉ์œผ๋กœ PostgreSQL+enum ์กฐํ•ฉ์—์„œ :param IS NULL ํšŒํ”ผ.
*/
@Cacheable(value = "storeList", key = "T(String).valueOf(#category) + ':' + T(String).valueOf(#district) + ':' + #page + ':' + #size", condition = "#name == null")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

SpEL(Spring Expression Language)์—์„œ null ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๊ณผ ์—ฐ๊ฒฐ(+)ํ•  ๋•Œ ์ž๋™์œผ๋กœ "null" ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋˜๋ฏ€๋กœ, T(String).valueOf(...)๋ฅผ ์ƒ๋žตํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.\n\n๋˜ํ•œ, ํ˜„์žฌ condition = "#name == null" ์กฐ๊ฑด์€ ๊ฒ€์ƒ‰์–ด(name)๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด("")์ด๋‚˜ ๊ณต๋ฐฑ(" ")์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ ์บ์‹œ๋ฅผ ํƒ€์ง€ ์•Š๊ณ  ํ•ญ์ƒ DB๋ฅผ ์กฐํšŒํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” name.isBlank()์ผ ๋•Œ trimmedName์„ null๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ „์ฒด ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋ฏ€๋กœ, ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋™์ผํ•œ ์ „์ฒด ๋ชฉ๋ก ์กฐํšŒ ์ฟผ๋ฆฌ๊ฐ€ ์บ์‹œ ์—†์ด ๋ฐ˜๋ณต ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋นˆ ๋ฌธ์ž์—ด์ด๋‚˜ ๊ณต๋ฐฑ์ธ ๊ฒฝ์šฐ์—๋„ ์บ์‹œ๊ฐ€ ์ ์šฉ๋˜๋„๋ก ์กฐ๊ฑด์„ ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

Suggested change
@Cacheable(value = "storeList", key = "T(String).valueOf(#category) + ':' + T(String).valueOf(#district) + ':' + #page + ':' + #size", condition = "#name == null")
@Cacheable(value = "storeList", key = "#category + ':' + #district + ':' + #page + ':' + #size", condition = "#name == null || #name.trim().isEmpty()")

@Transactional(readOnly = true)
public List<StoreListResponse> getStores(String name, Category category, District district, int page, int size) {
int limitedSize = Math.min(size, 100);
Expand All @@ -79,10 +81,9 @@ public List<StoreListResponse> getPopularStores(int limit) {
.toList();
}

@CacheEvict(value = "popularStores", allEntries = true)
@CacheEvict(value = {"popularStores", "storeList"}, allEntries = true)
@Transactional
public void evictPopularStoresCache() {
// ๋งค์žฅ ๋“ฑ๋ก/์ˆ˜์ •/๋ฆฌ๋ทฐ ์ƒ์„ฑ ๋“ฑ ์ธ๊ธฐ๋„ ๋ณ€๋™ ์‹œ ํ˜ธ์ถœ
}

@Tool(description = "์‚ฌ์šฉ์ž ์ฃผ๋ณ€์˜ ์ธ๊ธฐ ๋งค์žฅ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. '๋‚ด ์ฃผ๋ณ€ ๋ง›์ง‘', '๊ทผ์ฒ˜ ์ธ๊ธฐ ๋งค์žฅ', '์ฃผ๋ณ€ ๋ง›์ง‘ ์ถ”์ฒœ' ๋“ฑ์˜ ์š”์ฒญ์— ์‚ฌ์šฉํ•˜์„ธ์š”. ์œ„์น˜ ์ •๋ณด๊ฐ€ ์—†์œผ๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
Expand Down Expand Up @@ -196,6 +197,7 @@ public StoreDetailResponse getStore(Long storeId) {
}

// ๋งค์žฅ ์ •๋ณด ์ˆ˜์ •
@CacheEvict(value = "storeList", allEntries = true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

๋งค์žฅ ์ •๋ณด๊ฐ€ ์ˆ˜์ •๋˜๋ฉด ์ธ๊ธฐ ๋งค์žฅ ๋ชฉ๋ก(popularStores) ์บ์‹œ์—๋„ ์˜ํ–ฅ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ธ๊ธฐ ๋งค์žฅ์— ํฌํ•จ๋œ ๋งค์žฅ์˜ ์ด๋ฆ„์ด๋‚˜ ์ด๋ฏธ์ง€๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์บ์‹œ๊ฐ€ ์ฆ‰์‹œ ๋น„์›Œ์ง€์ง€ ์•Š์œผ๋ฉด ์‚ฌ์šฉ์ž๋Š” ์ตœ๋Œ€ 5๋ถ„ ๋™์•ˆ ์ด์ „ ์ •๋ณด๋ฅผ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.\n\n๋”ฐ๋ผ์„œ storeList๋ฟ๋งŒ ์•„๋‹ˆ๋ผ popularStores ์บ์‹œ๋„ ํ•จ๊ป˜ ๋น„์›Œ์ฃผ์–ด์•ผ ์บ์‹œ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Suggested change
@CacheEvict(value = "storeList", allEntries = true)
@CacheEvict(value = {"storeList", "popularStores"}, allEntries = true)

@Transactional
public StoreUpdateResponse updateStore(Long userId, Long storeId, StoreUpdateRequest request) {
userRepository.getAdminOrThrow(userId, ErrorCode.ADMIN_ONLY_STORE_UPDATE);
Expand All @@ -210,6 +212,7 @@ public StoreUpdateResponse updateStore(Long userId, Long storeId, StoreUpdateReq
}

// ๋งค์žฅ ์ƒํƒœ ๋ณ€๊ฒฝ
@CacheEvict(value = "storeList", allEntries = true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

๋งค์žฅ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด ๋น„ํ™œ์„ฑํ™”(INACTIVE, ์ฆ‰ soft delete)๋˜๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๋งค์žฅ์€ ์ธ๊ธฐ ๋งค์žฅ ๋ชฉ๋ก(popularStores)์—์„œ๋„ ์ฆ‰์‹œ ์ œ์™ธ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์บ์‹œ๊ฐ€ ๋งŒ๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ญ์ œ๋œ ๋งค์žฅ์„ ์ธ๊ธฐ ๋งค์žฅ ๋ชฉ๋ก์—์„œ ๋ณด๊ฒŒ ๋˜๊ณ , ํด๋ฆญ ์‹œ 404 ์—๋Ÿฌ๋ฅผ ๋งˆ์ฃผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.\n\n๋”ฐ๋ผ์„œ storeList๋ฟ๋งŒ ์•„๋‹ˆ๋ผ popularStores ์บ์‹œ๋„ ํ•จ๊ป˜ ๋น„์›Œ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Suggested change
@CacheEvict(value = "storeList", allEntries = true)
@CacheEvict(value = {"storeList", "popularStores"}, allEntries = true)

@Transactional
public StoreStatusUpdateResponse updateStoreStatus(Long userId, Long storeId, StoreStatusUpdateRequest request) {
userRepository.getAdminOrThrow(userId, ErrorCode.ADMIN_ONLY_STORE_STATUS);
Expand All @@ -223,20 +226,23 @@ public StoreStatusUpdateResponse updateStoreStatus(Long userId, Long storeId, St
* ์ž์ฒด ๋ฆฌ๋ทฐ ์ƒ์„ฑ ์‹œ ํ˜ธ์ถœ โ€” ์™ธ๋ถ€ ์‹œ๋“œ๋œ ๋ณ„์ /๋ฆฌ๋ทฐ์ˆ˜๋ฅผ base๋กœ ํ‰๊ท ์— ํ•ฉ์‚ฐ.
* ๋ฆฌ์Šค๋„ˆ์—์„œ ๋น„๋™๊ธฐ๋กœ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ๋ฆฌ๋ทฐ ์ž‘์„ฑ ์ž์ฒด์—” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.
*/
@CacheEvict(value = {"popularStores", "storeList"}, allEntries = true)
@Transactional
public void applyReviewCreated(Long storeId, int newStar) {
Store store = storeRepository.findByIdAndIsDeletedFalse(storeId)
.orElseThrow(() -> new CustomException(ErrorCode.STORE_NOT_FOUND));
store.applyReviewCreated(newStar);
}

@CacheEvict(value = {"popularStores", "storeList"}, allEntries = true)
@Transactional
public void applyReviewDeleted(Long storeId, int deletedStar) {
Store store = storeRepository.findByIdAndIsDeletedFalse(storeId)
.orElseThrow(() -> new CustomException(ErrorCode.STORE_NOT_FOUND));
store.applyReviewDeleted(deletedStar);
}

@CacheEvict(value = {"popularStores", "storeList"}, allEntries = true)
@Transactional
public void applyReviewUpdated(Long storeId, int oldStar, int newStar) {
Store store = storeRepository.findByIdAndIsDeletedFalse(storeId)
Expand Down
Loading