-
Notifications
You must be signed in to change notification settings - Fork 1
[REFACTOR] 지도 화면 가맹점 조회 로직 개선 #121
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
Changes from all commits
807ca07
a8898ca
d4d26ca
7c77e22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.kkinikong.be.cache.service; | ||
|
|
||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import com.kkinikong.be.store.domain.Store; | ||
| import com.kkinikong.be.store.repository.store.StoreRepository; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class StoreCacheService { | ||
|
|
||
| private final StoreRepository storeRepository; | ||
| private final RedisTemplateCacheService redisTemplateCacheService; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public String syncStoreLocations() { | ||
| log.info("가맹점 위치 정보 전수 동기화 시작..."); | ||
|
|
||
| int pageSize = 1000; | ||
| int pageNumber = 0; | ||
| long totalCount = 0; | ||
|
|
||
| while (true) { | ||
| Page<Store> storePage = storeRepository.findAll(PageRequest.of(pageNumber, pageSize)); | ||
|
|
||
| if (storePage.isEmpty()) break; | ||
|
|
||
| redisTemplateCacheService.saveStoreLocationsBulk(storePage.getContent()); | ||
| totalCount += storePage.getNumberOfElements(); | ||
| log.info("{}건 적재 완료...", totalCount); | ||
|
|
||
| if (!storePage.hasNext()) break; | ||
| pageNumber++; | ||
| } | ||
| return "성공적으로 " + totalCount + " 건의 위치 데이터를 동기화했습니다."; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,18 +18,22 @@ | |
| import org.apache.commons.csv.CSVParser; | ||
| import org.apache.commons.csv.CSVRecord; | ||
|
|
||
| import com.kkinikong.be.cache.service.RedisTemplateCacheService; | ||
| import com.kkinikong.be.store.domain.Store; | ||
| import com.kkinikong.be.store.domain.type.Category; | ||
| import com.kkinikong.be.store.dto.response.StoreUploadResponse; | ||
| import com.kkinikong.be.store.exception.StoreException; | ||
| import com.kkinikong.be.store.exception.errorcode.StoreErrorCode; | ||
| import com.kkinikong.be.store.repository.store.StoreRepository; | ||
| import com.kkinikong.be.store.repository.storeupload.StoreJdbcRepository; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class StoreUploadService { | ||
| private final StoreJdbcRepository storeJdbcRepository; | ||
| private final StoreRepository storeRepository; | ||
| private final RedisTemplateCacheService redisTemplateCacheService; | ||
|
|
||
| @Transactional | ||
| public StoreUploadResponse upload(MultipartFile file) { | ||
|
|
@@ -40,12 +44,24 @@ public StoreUploadResponse upload(MultipartFile file) { | |
| // 파일의 첫 번째 데이터에서 지역 정보 추출 | ||
| String targetRegion = newStores.get(0).getRegion(); | ||
|
|
||
| // 삭제될 기존 데이터의 ID 확보 | ||
| List<Long> oldStoreIds = storeRepository.findIdsByRegion(targetRegion); | ||
|
|
||
| // 기존 데이터는 유지하며 정보 갱신, 신규 데이터는 추가 | ||
| storeJdbcRepository.upsertStores(newStores); | ||
|
|
||
| // 새로운 파일에 없는 가맹점 삭제 | ||
| storeJdbcRepository.deleteMissingStores(targetRegion); | ||
|
|
||
| // redis에 해당 지역의 기존 데이터 삭제 | ||
| redisTemplateCacheService.removeStoreLocationsBulk(oldStoreIds); | ||
|
|
||
| // 최신화된 해당 지역 데이터 조회 | ||
| List<Store> updatedStores = storeRepository.findByRegion(targetRegion); | ||
|
|
||
| // redis에 최신 데이터 저장 | ||
| redisTemplateCacheService.saveStoreLocationsBulk(updatedStores); | ||
|
|
||
|
Comment on lines
+47
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 새로운 csv파일을 업로드 할 때 해당 지역의 기존 redis에 있던 데이터는 전부 삭제하고 새로운 csv 파일의 데이터를 넣는거라구 보면 될까??! 어차피 csv 파일 올리는건 횟수가 적으니까 상관없다고 생각하는뎅 그냥 궁금한점이야!!!ㅎㅎ
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞아 ! 우리 csv파일이 '지역'을 기준으로 되어있어서, 그 '지역'에 해당하는 데이터를 다 지우고 다시 새로 업로드 하는 방식이야 ! |
||
| return new StoreUploadResponse(newStores.size(), newStores.size()); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 때 중복저장 등의 문제는 없는지 궁금해용~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorted Set은 동일한 id를 중복해서 가질 수 없대 ! 동일한 id로 데이터를 넣으려고 하면, 덮어쓰기 작업이 일어날 뿐 데이터가 중복되어 늘어나지 않는다고 합니당 !