|
| 1 | +package com.example.log4u.domain.map.cache.dao; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import org.springframework.data.redis.core.RedisTemplate; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | + |
| 11 | +import com.example.log4u.domain.map.cache.CacheKeyGenerator; |
| 12 | +import com.example.log4u.domain.map.cache.RedisTTLPolicy; |
| 13 | +import com.example.log4u.domain.map.dto.response.DiaryMarkerResponseDto; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | + |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | + |
| 19 | +@Component |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Slf4j |
| 22 | +public class DiaryCacheDao { |
| 23 | + |
| 24 | + private final RedisTemplate<String, Object> redisTemplate; |
| 25 | + private final ObjectMapper objectMapper; |
| 26 | + |
| 27 | + public Set<Long> getDiaryIdSetFromCache(String geohash) { |
| 28 | + String key = CacheKeyGenerator.diaryIdSetKey(geohash); |
| 29 | + try { |
| 30 | + Set<Object> raw = redisTemplate.opsForSet().members(key); |
| 31 | + if (raw == null) |
| 32 | + return Collections.emptySet(); |
| 33 | + |
| 34 | + return raw.stream() |
| 35 | + .map(obj -> Long.parseLong(obj.toString())) |
| 36 | + .collect(Collectors.toSet()); |
| 37 | + } catch (Exception e) { |
| 38 | + log.error("diaryId Set 조회 실패 (key: {})", key, e); |
| 39 | + return Collections.emptySet(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void cacheDiaryIdSetByGeohash(String geohash, List<Long> diaryIds) { |
| 44 | + if (diaryIds == null || diaryIds.isEmpty()) |
| 45 | + return; |
| 46 | + |
| 47 | + String key = CacheKeyGenerator.diaryIdSetKey(geohash); |
| 48 | + try { |
| 49 | + redisTemplate.opsForSet().add(key, diaryIds.toArray()); |
| 50 | + redisTemplate.expire(key, RedisTTLPolicy.DIARY_ID_SET_TTL); |
| 51 | + } catch (Exception e) { |
| 52 | + log.error("diaryId Set 저장 실패 (key: {})", key, e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public DiaryMarkerResponseDto getDiaryFromCache(Long diaryId) { |
| 57 | + String key = CacheKeyGenerator.diaryKey(diaryId); |
| 58 | + try { |
| 59 | + Object raw = redisTemplate.opsForValue().get(key); |
| 60 | + if (raw == null) return null; |
| 61 | + |
| 62 | + return objectMapper.readValue(raw.toString(), DiaryMarkerResponseDto.class); |
| 63 | + } catch (Exception e) { |
| 64 | + log.error("단건 diary 조회 실패 (key: {})", key, e); |
| 65 | + return null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void cacheDiary(Long diaryId, DiaryMarkerResponseDto dto) { |
| 70 | + String key = CacheKeyGenerator.diaryKey(diaryId); |
| 71 | + try { |
| 72 | + String json = objectMapper.writeValueAsString(dto); |
| 73 | + redisTemplate.opsForValue().set(key, json, RedisTTLPolicy.DIARY_TTL); |
| 74 | + } catch (Exception e) { |
| 75 | + log.error("단건 diary 캐시 저장 실패 (key: {})", key, e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void evictDiaryIdFromCache(String geohash, Long diaryId) { |
| 80 | + String key = CacheKeyGenerator.diaryIdSetKey(geohash); |
| 81 | + try { |
| 82 | + redisTemplate.opsForSet().remove(key, diaryId); |
| 83 | + } catch (Exception e) { |
| 84 | + log.error("diaryId 제거 실패 (key: {}, diaryId: {})", key, diaryId, e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void evictDiaryFromCache(Long diaryId) { |
| 89 | + String key = CacheKeyGenerator.diaryKey(diaryId); |
| 90 | + try { |
| 91 | + redisTemplate.delete(key); |
| 92 | + } catch (Exception e) { |
| 93 | + log.error("diary 캐시 삭제 실패 (key: {})", key, diaryId, e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments