From 7cb9976ad54b2c3322524884bf1507890eaff4ca Mon Sep 17 00:00:00 2001 From: gomin0 Date: Wed, 18 Sep 2024 03:17:47 +0900 Subject: [PATCH] =?UTF-8?q?:ambulance:=20hotfix:=20redis=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EC=97=90=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 -- .../domain/accounts/entity/Profile.java | 2 +- .../service/account/AccountsService.java | 3 ++ .../service/FcmNotificationService.java | 31 ++----------------- .../BnagFer/global/config/RedisConfig.java | 9 ------ .../BnagFer/global/util/RedisUtil.java | 11 ++++--- 6 files changed, 13 insertions(+), 45 deletions(-) diff --git a/build.gradle b/build.gradle index e16fe671..93a80ed2 100644 --- a/build.gradle +++ b/build.gradle @@ -70,8 +70,6 @@ dependencies { //firebase implementation 'com.google.firebase:firebase-admin:9.2.0' - implementation 'org.redisson:redisson-spring-boot-starter:3.27.0' - //S3 implementation group: 'io.awspring.cloud', name: 'spring-cloud-starter-aws', version: '2.4.4' implementation group: 'io.awspring.cloud', name: 'spring-cloud-starter-aws-secrets-manager-config', version: '2.4.4' diff --git a/src/main/java/com/capstone/BnagFer/domain/accounts/entity/Profile.java b/src/main/java/com/capstone/BnagFer/domain/accounts/entity/Profile.java index c673e074..2fbf83ef 100644 --- a/src/main/java/com/capstone/BnagFer/domain/accounts/entity/Profile.java +++ b/src/main/java/com/capstone/BnagFer/domain/accounts/entity/Profile.java @@ -27,7 +27,7 @@ public class Profile extends BaseEntity { @Column(name = "profile_id") private Long id; - @Column(name = "nickname", nullable = false, length = 20) + @Column(name = "nickname", nullable = false, unique = true, length = 20) private String nickname; // 닉네임 @Column(name = "profile_image_url") diff --git a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java index 32e44e86..57d85e18 100644 --- a/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java +++ b/src/main/java/com/capstone/BnagFer/domain/accounts/service/account/AccountsService.java @@ -13,6 +13,8 @@ import io.jsonwebtoken.ExpiredJwtException; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -32,6 +34,7 @@ public class AccountsService { private final JwtProvider jwtProvider; private final RedisUtil redisUtil; private final AccountsCommonService accountsCommonService; + private static final Logger logger = LoggerFactory.getLogger(AccountsService.class); public UserLoginResponseDto login(UserLoginRequestDto requestDto) { diff --git a/src/main/java/com/capstone/BnagFer/domain/notification/service/FcmNotificationService.java b/src/main/java/com/capstone/BnagFer/domain/notification/service/FcmNotificationService.java index 1ec83e3e..b790bff0 100644 --- a/src/main/java/com/capstone/BnagFer/domain/notification/service/FcmNotificationService.java +++ b/src/main/java/com/capstone/BnagFer/domain/notification/service/FcmNotificationService.java @@ -13,13 +13,10 @@ import com.google.firebase.messaging.Message; import com.google.firebase.messaging.Notification; import lombok.RequiredArgsConstructor; -import org.redisson.api.RLock; -import org.redisson.api.RedissonClient; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import java.util.concurrent.TimeUnit; @RequiredArgsConstructor @Service @@ -29,36 +26,12 @@ public class FcmNotificationService { private final UserJpaRepository userJpaRepository; private final RedisUtil redisUtil; private final FcmNotificationRepository fcmNotificationRepository; - private final RedissonClient redissonClient; - - private static final String LOCK_PREFIX = "fcm_notification:"; // 항상 새로운 트랜잭션에서 실행 @Transactional(propagation = Propagation.REQUIRES_NEW) public FcmNotification saveNotification(FcmNotificationRequestDto requestDto, User user) { - String lockKey = LOCK_PREFIX + user.getId(); - RLock lock = redissonClient.getLock(lockKey); - - try { - // 5초 동안 락 획득 시도, 획득 성공 시 최대 10초 동안 락 유지 => 데드락 방지 - - if (lock.tryLock(5, 10, TimeUnit.SECONDS)) { - try { - FcmNotification fcmNotification = requestDto.toEntity(user); - return fcmNotificationRepository.save(fcmNotification); - } finally { - // 작업 완료 후 즉시 락 해제 시도 - if (lock.isHeldByCurrentThread()) { - lock.unlock(); - } - } - } else { - throw new FcmNotificationExceptionHandler(ErrorCode.LOCK_ACQUISITION_FAILED); - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new FcmNotificationExceptionHandler(ErrorCode.NOTIFICATION_SAVE_FAILED); - } + FcmNotification fcmNotification = requestDto.toEntity(user); + return fcmNotificationRepository.save(fcmNotification); } // 트랜잭션 없이 실행 diff --git a/src/main/java/com/capstone/BnagFer/global/config/RedisConfig.java b/src/main/java/com/capstone/BnagFer/global/config/RedisConfig.java index 490a2675..85f67ee9 100644 --- a/src/main/java/com/capstone/BnagFer/global/config/RedisConfig.java +++ b/src/main/java/com/capstone/BnagFer/global/config/RedisConfig.java @@ -1,8 +1,5 @@ package com.capstone.BnagFer.global.config; -import org.redisson.Redisson; -import org.redisson.api.RedissonClient; -import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,10 +31,4 @@ public RedisTemplate redisTemplate() { return redisTemplate; } - @Bean - public RedissonClient redissonClient() { - Config config = new Config(); - config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort); - return Redisson.create(config); - } } diff --git a/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java b/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java index ef1ace2d..db4d627a 100644 --- a/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java +++ b/src/main/java/com/capstone/BnagFer/global/util/RedisUtil.java @@ -9,6 +9,7 @@ @RequiredArgsConstructor public class RedisUtil { private final RedisTemplate redisTemplate; + private static final String FCM_TOKEN_PREFIX = "fcm:token:"; private static final long ONE_WEEK_IN_SECONDS = 7 * 24 * 60 * 60; // 일주일을 초로 표현 public void save(String key, Object val, Long time, TimeUnit timeUnit) { @@ -68,12 +69,13 @@ public boolean delete(String key) { } public void saveFCMToken(String userEmail, String fcmToken) { - redisTemplate.opsForValue().set(userEmail, fcmToken); - redisTemplate.expire(userEmail, 30, TimeUnit.DAYS); + String key = FCM_TOKEN_PREFIX + userEmail; + redisTemplate.opsForValue().set(key, fcmToken, 30, TimeUnit.DAYS); } public String getFCMToken(String userEmail) { - Object tokenObj = redisTemplate.opsForValue().get(userEmail); + String key = FCM_TOKEN_PREFIX + userEmail; + Object tokenObj = redisTemplate.opsForValue().get(key); if (tokenObj != null) { return (String) tokenObj; } else { @@ -82,6 +84,7 @@ public String getFCMToken(String userEmail) { } public void removeFCMToken(String userEmail) { - redisTemplate.delete(userEmail); + String key = FCM_TOKEN_PREFIX + userEmail; + redisTemplate.delete(key); } } \ No newline at end of file