11package org .dfbf .soundlink .domain .emotionRecord .service ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import feign .Response ;
35import jakarta .persistence .OptimisticLockException ;
46import lombok .RequiredArgsConstructor ;
57import lombok .extern .slf4j .Slf4j ;
2628import org .springframework .data .domain .PageRequest ;
2729import org .springframework .data .domain .Pageable ;
2830import org .springframework .data .domain .Sort ;
31+ import org .springframework .data .redis .core .RedisTemplate ;
2932import org .springframework .retry .annotation .Backoff ;
3033import org .springframework .retry .annotation .Recover ;
3134import org .springframework .retry .annotation .Retryable ;
3235import org .springframework .stereotype .Service ;
3336import org .springframework .transaction .annotation .Transactional ;
3437
3538import java .util .List ;
39+ import java .util .concurrent .TimeUnit ;
3640
3741@ Service
3842@ RequiredArgsConstructor
@@ -47,6 +51,8 @@ public class EmotionRecordService {
4751 // private final EmotionRecordCacheService emotionRecordCacheService;
4852 private final ChatRoomRepository chatRoomRepository ;
4953
54+ private final RedisTemplate <String , Object > redisTemplate ;
55+
5056 @ Transactional
5157 public ResponseResult saveEmotionRecordWithMusic (Long userId , EmotionRecordRequestDTO request ) {
5258
@@ -74,8 +80,8 @@ public ResponseResult saveEmotionRecordWithMusic(Long userId, EmotionRecordReque
7480 .build ();
7581 emotionRecordRepository .save (emotionRecord );
7682
77- /* // 게시글 생성 시, 해당 조건에 맞는 캐시 키 삭제
78- emotionRecordCacheService.evictEmotionRecordCache(
83+ // 게시글 생성 시, 해당 조건에 맞는 캐시 키 삭제
84+ /* emotionRecordCacheService.evictEmotionRecordCache(
7985 userId,
8086 request.spotifyId(),
8187 request.emotion().name()
@@ -95,6 +101,21 @@ public ResponseResult saveEmotionRecordWithMusic(Long userId, EmotionRecordReque
95101 }
96102 }
97103
104+ // 감정일기 추가
105+ // 중복 요청 방지 기능 추가 (멱등성 보장)
106+ @ Transactional
107+ public ResponseResult saveEmotionRecordWithMusicAndIdempotent (String idempotencyKey , Long userId , EmotionRecordRequestDTO request ) {
108+ if (!redisTemplate .keys ("EmotionRecord:" + idempotencyKey ).isEmpty ()) {
109+ ObjectMapper mapper = new ObjectMapper ();
110+ return mapper .convertValue (redisTemplate .opsForValue ().get ("EmotionRecord:" + idempotencyKey ), ResponseResult .class );
111+ }
112+
113+ ResponseResult responseResult = saveEmotionRecordWithMusic (userId , request );
114+ redisTemplate .opsForValue ().set ("EmotionRecord:" + idempotencyKey , responseResult , 10 , TimeUnit .SECONDS );
115+
116+ return responseResult ;
117+ }
118+
98119 @ Transactional (readOnly = true )
99120 public ResponseResult getEmotionRecordsByUserId (Long userId , int page , int size ) {
100121 ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
@@ -122,7 +143,6 @@ public ResponseResult getEmotionRecordsByUserId(Long userId, int page, int size)
122143 }
123144 }
124145
125-
126146 @ Transactional (readOnly = true )
127147 public ResponseResult getEmotionRecordsByLoginId (String userTag , int page , int size ) {
128148 ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
0 commit comments