Skip to content

Commit

Permalink
refactor: 낙관적 락 Aspect 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jemin committed Dec 28, 2023
1 parent 216f3aa commit 132b2a1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Slf4j
@Aspect
@Component
@Order(Ordered.LOWEST_PRECEDENCE - 2) // @Transaction보다 먼저 AOP가 호출
@Order(Ordered.LOWEST_PRECEDENCE - 2)
@RequiredArgsConstructor
public class OptimisticLockAspect {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public ResponseEntity<ApiResponse<Void>> updateGroup(@PathVariable Long groupId,
public ResponseEntity<ApiResponse<Void>> joinGroup(@CurrentUser LoginUser loginUser,
@PathVariable(name = "groupId") Long groupId) {

groupService.joinGroup(loginUser.getId(), groupId);
int i = groupService.joinGroup(loginUser.getId(), groupId);
System.out.println(i);
return ApiResponse.success(SuccessCode.INSERT_SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmc.mellyserver.controller.health;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthCheckController {

@GetMapping("/api/health")
public ResponseEntity<String> healthCheck() {
return ResponseEntity.ok("health up");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public class CommentLikeService {

private final CommentReader commentReader;

@DistributedLock(key = "#commentId") // 대부분은 분산락으로 처리
@OptimisticLock(retryCount = 3, waitTime = 1000L) // 만약의 경우를 대비해서 낙관적락도 걸기
@OptimisticLock
@DistributedLock(key = "#commentId")
@Transactional
public void saveCommentLike(final Long userId, final Long commentId) {

Comment comment = commentReader.findByIdWithLock(commentId); // 댓글 조회 (비관적 락)
commentLikeValidator.validateDuplicatedLike(commentId, userId); // commentLike 테이블 조회 -> 자신이 좋아요를 눌렀는지 기록 필요
Comment comment = commentReader.findByIdWithLock(commentId);
commentLikeValidator.validateDuplicatedLike(commentId, userId);
comment.addLike();
commentLikeWriter.save(userId, comment);
}

@DistributedLock(key = "#commentId") // 대부분은 분산락으로 처리
@OptimisticLock(retryCount = 3, waitTime = 1000L) // 만약의 경우를 대비해서 낙관적락도 걸기
@OptimisticLock
@DistributedLock(key = "#commentId")
@Transactional
public void deleteCommentLike(final Long userId, final Long commentId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ public Long saveGroup(Long userId, CreateGroupRequestDto createGroupRequestDto)
@OptimisticLock
@CacheEvict(cacheNames = CacheNames.GROUP, key = "#groupId")
@Transactional
public void joinGroup(final Long userId, final Long groupId) {
public int joinGroup(final Long userId, final Long groupId) {

UserGroup userGroup = groupReader.readWithLock(groupId);
groupValidator.isMaximumGroupMember(groupId);
groupValidator.isDuplicatedJoin(userId, userGroup.getId());
groupAndUserWriter.save(GroupAndUser.of(userId, userGroup));
return 1;
}

@CacheEvict(cacheNames = CacheNames.GROUP, key = "#updateGroupRequestDto.groupId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress(REDISSON_HOST_PREFIX + host + ":" + port)
.setRetryAttempts(2)
.setRetryInterval(500);
.setConnectTimeout(3000);

return Redisson.create(config);
}
Expand Down

0 comments on commit 132b2a1

Please sign in to comment.