11package com .example .log4u .domain .map .cache .dao ;
22
3- import java .time .Duration ;
3+ import static com .example .log4u .common .config .redis .ObjectMapperFactory .*;
4+
45import java .util .List ;
5- import java .util .Optional ;
66
7- import org .springframework .data .redis .core .RedisTemplate ;
87import org .springframework .stereotype .Component ;
98
9+ import com .example .log4u .common .infra .cache .CacheManager ;
1010import com .example .log4u .domain .map .cache .CacheKeyGenerator ;
1111import com .example .log4u .domain .map .cache .RedisTTLPolicy ;
1212import com .example .log4u .domain .map .dto .response .DiaryClusterResponseDto ;
1313import com .example .log4u .domain .map .exception .InvalidMapLevelException ;
14- import com .example .log4u .domain .map .repository .sido .SidoAreasDiaryCountRepository ;
1514import com .example .log4u .domain .map .repository .sido .SidoAreasRepository ;
16- import com .example .log4u .domain .map .repository .sigg .SiggAreasDiaryCountRepository ;
1715import com .example .log4u .domain .map .repository .sigg .SiggAreasRepository ;
18- import com .fasterxml .jackson .databind . ObjectMapper ;
16+ import com .fasterxml .jackson .core . type . TypeReference ;
1917
2018import lombok .RequiredArgsConstructor ;
2119import lombok .extern .slf4j .Slf4j ;
2220
23- import com .fasterxml .jackson .core .type .TypeReference ;
24-
25-
2621@ Component
2722@ RequiredArgsConstructor
2823@ Slf4j
2924public 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}
0 commit comments