Skip to content

Commit 0aaa6e3

Browse files
committed
refactor: 다이어리 생성 시 geohash 문자열 저장 기능 추가
- Look-Aside 전략에 따라 생성 시 캐시에는 저장하지 않음
1 parent 3dddd37 commit 0aaa6e3

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/main/java/com/example/log4u/domain/diary/facade/DiaryFacade.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.example.log4u.domain.diary.dto.PopularDiaryDto;
1414
import com.example.log4u.domain.diary.entity.Diary;
1515
import com.example.log4u.domain.diary.service.DiaryService;
16+
import com.example.log4u.domain.diary.service.DiaryGeohashService;
1617
import com.example.log4u.domain.hashtag.service.HashtagService;
1718
import com.example.log4u.domain.like.service.LikeService;
1819
import com.example.log4u.domain.map.service.MapService;
@@ -33,6 +34,7 @@ public class DiaryFacade {
3334
private final LikeService likeService;
3435
private final HashtagService hashtagService;
3536
private final UserService userService;
37+
private final DiaryGeohashService diaryGeohashService;
3638

3739
/**
3840
* 다이어리 생성 use case
@@ -41,23 +43,30 @@ public class DiaryFacade {
4143
* 2. diaryService: 다이어리 생성<br>
4244
* 2. mediaService: 해당 다이어리의 이미지 저장<br>
4345
* 3. mapService: 해당 구역 카운트 증가
46+
* 4. diaryGeohashService: 해당 다이어리 위치가 포함되어있는 geoHash 문자열 저장
4447
* */
4548
@Transactional
4649
public void createDiary(Long userId, DiaryRequestDto request) {
4750
String thumbnailUrl = mediaService.extractThumbnailUrl(request.mediaList());
4851
Diary diary = diaryService.saveDiary(userId, request, thumbnailUrl);
4952
mediaService.saveMedia(diary.getDiaryId(), request.mediaList());
5053
hashtagService.saveOrUpdateHashtag(diary.getDiaryId(), request.hashtagList());
51-
mapService.increaseRegionDiaryCount(request.location().latitude(), request.location().longitude());
54+
55+
double lat = request.location().latitude();
56+
double lon = request.location().longitude();
57+
mapService.increaseRegionDiaryCount(lat, lon);
58+
diaryGeohashService.saveGeohash(diary.getDiaryId(), lat, lon);
5259
}
5360

5461
/**
5562
* 다이어리 삭제 use case
5663
* <ul><li>호출 과정</li></ul>
5764
* 1. diaryService: 다이어리 검증
5865
* 2. mediaService: 해당 다이어리 이미지 삭제<br>
59-
* 3. diaryService: 다이어리 삭제<br>
60-
* 4. mapService: 해당 구역 카운트 감소
66+
* 3. mapService: 해당 구역 카운트 감소
67+
* 4. diaryGeohashService: 캐싱 되어있는 id, 데이터 삭제
68+
* 5. diaryService: 다이어리 삭제<br>
69+
*
6170
* */
6271
@Transactional
6372
public void deleteDiary(Long userId, Long diaryId) {
@@ -66,6 +75,7 @@ public void deleteDiary(Long userId, Long diaryId) {
6675
hashtagService.deleteHashtagsByDiaryId(diaryId);
6776
mapService.decreaseRegionDiaryCount(diary.getLocation().getLatitude(), diary.getLocation().getLongitude());
6877

78+
diaryGeohashService.deleteGeohashAndCache(diaryId);
6979
diaryService.deleteDiary(diary);
7080
}
7181

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.example.log4u.domain.diary.service;
2+
3+
import java.util.List;
4+
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
import com.example.log4u.domain.diary.entity.DiaryGeoHash;
9+
import com.example.log4u.domain.diary.repository.DiaryGeoHashRepository;
10+
import com.example.log4u.domain.map.cache.dao.DiaryCacheDao;
11+
12+
import ch.hsr.geohash.GeoHash;
13+
import lombok.RequiredArgsConstructor;
14+
import lombok.extern.slf4j.Slf4j;
15+
16+
@Service
17+
@RequiredArgsConstructor
18+
@Slf4j
19+
public class DiaryGeohashService {
20+
21+
private final DiaryGeoHashRepository diaryGeoHashRepository;
22+
private final DiaryCacheDao diaryCacheDao;
23+
24+
@Transactional
25+
public void saveGeohash(Long diaryId, double lat, double lon) {
26+
String hash = GeoHash.withCharacterPrecision(lat, lon, 5).toBase32();
27+
DiaryGeoHash diaryGeoHash = DiaryGeoHash.builder()
28+
.diaryId(diaryId)
29+
.geohash(hash)
30+
.build();
31+
diaryGeoHashRepository.save(diaryGeoHash);
32+
}
33+
34+
@Transactional
35+
public void deleteGeohashAndCache(Long diaryId) {
36+
DiaryGeoHash geoHash = diaryGeoHashRepository.findByDiaryId(diaryId);
37+
diaryCacheDao.evictDiaryIdFromCache(geoHash.getGeohash(), diaryId);
38+
diaryCacheDao.evictDiaryFromCache(diaryId);
39+
40+
diaryGeoHashRepository.deleteById(diaryId);
41+
}
42+
43+
public List<Long> getDiaryIdsByGeohash(String geohash) {
44+
return diaryGeoHashRepository.findDiaryIdByGeohash(geohash);
45+
}
46+
}

0 commit comments

Comments
 (0)