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,23 @@ 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 (idempotencyKey != null && !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+ if (idempotencyKey != null ) {
115+ redisTemplate .opsForValue ().set ("EmotionRecord:" + idempotencyKey , responseResult , 10 , TimeUnit .SECONDS );
116+ }
117+
118+ return responseResult ;
119+ }
120+
98121 @ Transactional (readOnly = true )
99122 public ResponseResult getEmotionRecordsByUserId (Long userId , int page , int size ) {
100123 ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
@@ -122,7 +145,6 @@ public ResponseResult getEmotionRecordsByUserId(Long userId, int page, int size)
122145 }
123146 }
124147
125-
126148 @ Transactional (readOnly = true )
127149 public ResponseResult getEmotionRecordsByLoginId (String userTag , int page , int size ) {
128150 ResponseResult pageValidationResult = validateAndCreatePageable (page , size );
0 commit comments