Skip to content

Commit

Permalink
#575 [feat] DistributedLock 템플릿 구현 및 활용
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyundoh committed Nov 13, 2024
1 parent 66a2224 commit 9d7f3a8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mile.common.lock;

import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.MileException;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Component
public class DistributedLock {

private final RedissonClient redissonClient;

public void getLock(final String key) {
final RLock lock = redissonClient.getLock(key);
try {
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public void afterLock(final String key) {
final RLock lock = redissonClient.getLock(key);
lock.unlock();
}

public void checkAvailability(final Boolean available) {
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION);
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package com.mile.moim.service.lock;
package com.mile.common.lock;

import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.MileException;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Aspect
@RequiredArgsConstructor
@Component
public class MoimNameRequestAspect {

private final RedissonClient redissonClient;
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK : ";
private final DistributedLock distributedLock;
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK";
private final AopForTransaction aopForTransaction;

@Pointcut("@annotation(com.mile.moim.service.lock.AtomicValidateUniqueMoimName)")
@Pointcut("@annotation(com.mile.common.lock.AtomicValidateUniqueMoimName)")
public void uniqueMoimNameCut() {
}

@Around("uniqueMoimNameCut()")
public Object validateUniqueName(final ProceedingJoinPoint joinPoint) throws Throwable {
final RLock lock = redissonClient.getLock(MOIM_NAME_LOCK);
try {
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS));
distributedLock.getLock(MOIM_NAME_LOCK);
return aopForTransaction.proceed(joinPoint);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
distributedLock.afterLock(MOIM_NAME_LOCK);
}
}

public void checkAvailability(final Boolean available) {
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mile.moim.domain.Moim;
import com.mile.moim.repository.MoimRepository;
import com.mile.moim.service.dto.response.MoimInfoResponse;
import com.mile.moim.service.lock.AtomicValidateUniqueMoimName;
import com.mile.common.lock.AtomicValidateUniqueMoimName;
import com.mile.user.domain.User;
import com.mile.common.utils.DateUtil;
import com.mile.writername.domain.WriterName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mile.moim.service.popular;

import com.mile.common.lock.DistributedLock;
import com.mile.curious.repository.dto.PostAndCuriousCountInLastWeek;
import com.mile.curious.service.CuriousRetriever;
import com.mile.moim.domain.Moim;
import com.mile.moim.domain.popular.MoimCuriousPost;
import com.mile.moim.domain.popular.MoimCuriousWriter;
import com.mile.moim.domain.popular.MoimPopularInfo;
import com.mile.moim.repository.MoimPopularInfoRepository;
import com.mile.moim.service.lock.AtomicMoimPopulerInfo;
import com.mile.writername.domain.WriterName;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
Expand All @@ -25,6 +25,7 @@ public class MoimPopularInfoRegister {

private final MoimPopularInfoRepository moimPopularInfoRepository;
private final CuriousRetriever curiousRetriever;
private final DistributedLock distributedLock;


private Set<MoimCuriousPost> getMoimCuriousPost(final List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek) {
Expand All @@ -46,6 +47,8 @@ private Set<MoimCuriousWriter> getMoimCuriousWriter(final List<PostAndCuriousCou

@CachePut(value = "moimPopularInfo", key = "#moim.id")
public MoimPopularInfo setMostPopularInfoOfMoim(final Moim moim) {
distributedLock.getLock("MOIM_POPULAR_LOCK");

List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek = curiousRetriever.findMostCuriousPostsInLastWeek(moim);

Set<MoimCuriousPost> moimCuriousPosts = getMoimCuriousPost(mostCuriousPostsInLastWeek);
Expand All @@ -54,7 +57,11 @@ public MoimPopularInfo setMostPopularInfoOfMoim(final Moim moim) {

MoimPopularInfo moimPopularInfo = MoimPopularInfo.of(moim.getId(), moimCuriousPosts, moimCuriousWriters);

return moimPopularInfoRepository.saveAndFlush(moimPopularInfo);
moimPopularInfoRepository.saveAndFlush(moimPopularInfo);

distributedLock.afterLock("MOIM_POPULAR_LOCK");

return moimPopularInfo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mile.moim.domain.Moim;
import com.mile.moim.domain.popular.MoimPopularInfo;
import com.mile.moim.repository.MoimPopularInfoRepository;
import com.mile.moim.service.lock.AtomicMoimPopulerInfo;
import com.mile.slack.module.SendMessageModule;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,7 +21,6 @@ public class MoimPopularInfoService {


@Cacheable(value = "moimPopularInfo", key = "#moim.id")
@AtomicMoimPopulerInfo
public MoimPopularInfo getMoimPopularInfo(final Moim moim) {
return moimPopularInfoRepository.findByMoimId(moim.getId()).orElseGet(
() -> moimPopularInfoRegister.setMostPopularInfoOfMoim(moim)
Expand Down

0 comments on commit 9d7f3a8

Please sign in to comment.