Skip to content

Commit b85d238

Browse files
committed
refactor: Redis 캐시 로직 추상화한 코드로 수정
1 parent 4c8af90 commit b85d238

File tree

3 files changed

+50
-64
lines changed

3 files changed

+50
-64
lines changed

src/main/java/com/example/log4u/domain/map/cache/RedisTTLPolicy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.time.Duration;
44

55
public class RedisTTLPolicy {
6-
public static final Duration DIARY_ID_SET_TTL = Duration.ofMinutes(60);
7-
public static final Duration DIARY_TTL = Duration.ofMinutes(60);
6+
public static final Duration MARKER_TTL = Duration.ofMinutes(60);
87
public static final Duration CLUSTER_TTL = Duration.ofMinutes(60);
98
}
Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,49 @@
11
package com.example.log4u.domain.map.cache.dao;
22

3-
import java.time.Duration;
3+
import static com.example.log4u.common.config.redis.ObjectMapperFactory.*;
4+
45
import java.util.List;
5-
import java.util.Optional;
66

7-
import org.springframework.data.redis.core.RedisTemplate;
87
import org.springframework.stereotype.Component;
98

9+
import com.example.log4u.common.infra.cache.CacheManager;
1010
import com.example.log4u.domain.map.cache.CacheKeyGenerator;
1111
import com.example.log4u.domain.map.cache.RedisTTLPolicy;
1212
import com.example.log4u.domain.map.dto.response.DiaryClusterResponseDto;
1313
import com.example.log4u.domain.map.exception.InvalidMapLevelException;
14-
import com.example.log4u.domain.map.repository.sido.SidoAreasDiaryCountRepository;
1514
import com.example.log4u.domain.map.repository.sido.SidoAreasRepository;
16-
import com.example.log4u.domain.map.repository.sigg.SiggAreasDiaryCountRepository;
1715
import com.example.log4u.domain.map.repository.sigg.SiggAreasRepository;
18-
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import com.fasterxml.jackson.core.type.TypeReference;
1917

2018
import lombok.RequiredArgsConstructor;
2119
import lombok.extern.slf4j.Slf4j;
2220

23-
import com.fasterxml.jackson.core.type.TypeReference;
24-
25-
2621
@Component
2722
@RequiredArgsConstructor
2823
@Slf4j
2924
public class ClusterCacheDao {
3025

31-
private final RedisTemplate<String, String> redisTemplate;
32-
private final ObjectMapper objectMapper;
26+
private final CacheManager cacheManager;
27+
3328
private final SidoAreasRepository sidoAreasRepository;
3429
private final SiggAreasRepository siggAreasRepository;
3530

3631
public List<DiaryClusterResponseDto> load(String geohash, int level) {
37-
String key = CacheKeyGenerator.clusterCacheKey(geohash, level);
38-
try {
39-
String value = redisTemplate.opsForValue().get(key);
40-
if (value == null){
41-
return null;
42-
}
43-
return objectMapper.readValue(value, new TypeReference<>() {
44-
});
45-
} catch (Exception e) {
46-
log.warn("[REDIS][CLUSTER][GET][ERR] key={}", key, e);
47-
throw new RuntimeException(e);
32+
String value = cacheManager.get(CacheKeyGenerator.clusterCacheKey(geohash, level));
33+
if (value == null) {
34+
return null;
4835
}
36+
return convertToClusters(value);
37+
}
38+
39+
private List<DiaryClusterResponseDto> convertToClusters(String value) {
40+
return readValue(value, new TypeReference<>() {
41+
});
4942
}
5043

5144
public List<DiaryClusterResponseDto> loadAndCache(String geohash, int level) {
5245
List<DiaryClusterResponseDto> clusters = loadClustersFromDb(geohash, level);
53-
cache(clusters,geohash, level);
46+
cache(clusters, geohash, level);
5447
return clusters;
5548
}
5649

@@ -64,13 +57,17 @@ private List<DiaryClusterResponseDto> loadClustersFromDb(String geohash, int lev
6457

6558
private void cache(List<DiaryClusterResponseDto> clusters, String geohash, int level) {
6659
String key = CacheKeyGenerator.clusterCacheKey(geohash, level);
60+
cacheManager.cache(key, writeValueAsString(clusters), RedisTTLPolicy.CLUSTER_TTL);
61+
}
6762

68-
try {
69-
String json = objectMapper.writeValueAsString(clusters);
70-
redisTemplate.opsForValue().set(key, json, RedisTTLPolicy.CLUSTER_TTL);
71-
} catch (Exception e) {
72-
log.warn("클러스터 캐시 저장 실패 (geo={}, level={})", key, clusters.size(), e);
73-
throw new RuntimeException(e);
74-
}
63+
public void evictSido(String geohash) {
64+
String key = CacheKeyGenerator.clusterCacheKey(geohash, 1);
65+
cacheManager.evict(key);
7566
}
67+
68+
public void evictSigg(String geohash) {
69+
String key = CacheKeyGenerator.clusterCacheKey(geohash, 2);
70+
cacheManager.evict(key);
71+
}
72+
7673
}
Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.example.log4u.domain.map.cache.dao;
22

3+
import static com.example.log4u.common.config.redis.ObjectMapperFactory.*;
4+
35
import java.util.Collections;
46
import java.util.List;
57

6-
import org.springframework.data.redis.core.RedisTemplate;
78
import org.springframework.stereotype.Component;
89

10+
import com.example.log4u.common.infra.cache.CacheManager;
911
import com.example.log4u.domain.diary.entity.Diary;
1012
import com.example.log4u.domain.diary.repository.DiaryGeoHashRepository;
1113
import com.example.log4u.domain.diary.repository.DiaryRepository;
1214
import com.example.log4u.domain.map.cache.CacheKeyGenerator;
1315
import com.example.log4u.domain.map.cache.RedisTTLPolicy;
1416
import com.example.log4u.domain.map.dto.response.DiaryMarkerResponseDto;
1517
import com.fasterxml.jackson.core.type.TypeReference;
16-
import com.fasterxml.jackson.databind.ObjectMapper;
1718

1819
import lombok.RequiredArgsConstructor;
1920
import lombok.extern.slf4j.Slf4j;
@@ -23,58 +24,47 @@
2324
@Slf4j
2425
public class MarkerCacheDao {
2526

26-
private final RedisTemplate<String, String> redisTemplate;
27-
private final ObjectMapper objectMapper;
27+
private final CacheManager cacheManager;
2828

2929
private final DiaryRepository diaryRepository;
3030
private final DiaryGeoHashRepository diaryGeoHashRepository;
3131

3232
public List<DiaryMarkerResponseDto> load(String geohash) {
33-
String key = CacheKeyGenerator.markerCacheKey(geohash);
34-
try {
35-
String value = redisTemplate.opsForValue().get(key);
36-
if (value == null) {
37-
return null;
38-
}
39-
return objectMapper.readValue(value, new TypeReference<>() {
40-
});
41-
} catch (Exception e) {
42-
log.warn("[REDIS][GET][ERR] key={}", key, e);
43-
throw new RuntimeException(e);
33+
String value = cacheManager.get(CacheKeyGenerator.markerCacheKey(geohash));
34+
if (value == null) {
35+
return null;
4436
}
37+
return convertToMarkers(value);
38+
}
39+
40+
private List<DiaryMarkerResponseDto> convertToMarkers(String value) {
41+
return readValue(value, new TypeReference<>() {
42+
});
4543
}
4644

4745
public List<DiaryMarkerResponseDto> loadAndCache(String geohash) {
46+
List<DiaryMarkerResponseDto> markers = loadMarkersFromDb(geohash);
47+
cache(markers, geohash);
48+
return markers;
49+
}
50+
51+
private List<DiaryMarkerResponseDto> loadMarkersFromDb(String geohash) {
4852
List<Long> diaryIds = diaryGeoHashRepository.findDiaryIdByGeohash(geohash);
4953
if (diaryIds.isEmpty()) {
50-
cache(Collections.emptyList(), geohash);
5154
return Collections.emptyList();
5255
}
5356
List<Diary> diaries = diaryRepository.findAllById(diaryIds);
54-
List<DiaryMarkerResponseDto> markers = diaries.stream()
57+
return diaries.stream()
5558
.map(DiaryMarkerResponseDto::of)
5659
.toList();
57-
cache(markers, geohash);
58-
return markers;
5960
}
6061

6162
private void cache(List<DiaryMarkerResponseDto> markers, String geohash) {
6263
String key = CacheKeyGenerator.markerCacheKey(geohash);
63-
try {
64-
String json = objectMapper.writeValueAsString(markers);
65-
redisTemplate.opsForValue().set(key, json, RedisTTLPolicy.DIARY_TTL);
66-
} catch (Exception e) {
67-
log.warn("[REDIS][SET][ERR] key={} count={}", key, markers.size(), e);
68-
throw new RuntimeException(e);
69-
}
64+
cacheManager.cache(key, writeValueAsString(markers), RedisTTLPolicy.MARKER_TTL);
7065
}
7166

7267
public void evict(String geohash) {
73-
String key = CacheKeyGenerator.markerCacheKey(geohash);
74-
try {
75-
redisTemplate.delete(key);
76-
} catch (Exception e) {
77-
throw new RuntimeException(e);
78-
}
68+
cacheManager.evict(CacheKeyGenerator.markerCacheKey(geohash));
7969
}
8070
}

0 commit comments

Comments
 (0)