-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: redisson distributed lock aop
- Loading branch information
Showing
16 changed files
with
186 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
server/src/main/java/com/fluffy/global/cache/LockManager.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
server/src/main/java/com/fluffy/global/cache/RedisConfig.java
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
server/src/main/java/com/fluffy/global/cache/RedisLockUtil.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
server/src/main/java/com/fluffy/global/redis/AopForTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
public class AopForTransaction { | ||
|
||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
public Object proceed(ProceedingJoinPoint joinPoint) throws Throwable { | ||
return joinPoint.proceed(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
server/src/main/java/com/fluffy/global/redis/CustomSpringELParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
|
||
public class CustomSpringELParser { | ||
|
||
private CustomSpringELParser() { | ||
} | ||
|
||
public static Object getDynamicValue(String[] parameterNames, Object[] args, String key) { | ||
ExpressionParser parser = new SpelExpressionParser(); | ||
StandardEvaluationContext context = new StandardEvaluationContext(); | ||
|
||
for (int i = 0; i < parameterNames.length; i++) { | ||
context.setVariable(parameterNames[i], args[i]); | ||
} | ||
|
||
return parser.parseExpression(key).getValue(context, Object.class); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
server/src/main/java/com/fluffy/global/redis/DistributedLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DistributedLock { | ||
|
||
String key(); | ||
|
||
TimeUnit timeUnit() default TimeUnit.SECONDS; | ||
|
||
/** | ||
* 락을 획득하기 위해 대기할 시간 | ||
*/ | ||
long waitTime() default 3L; | ||
|
||
/** | ||
* 락을 유지할 시간 | ||
*/ | ||
long leaseTime() default 3L; | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/main/java/com/fluffy/global/redis/DistributedLockAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class DistributedLockAspect { | ||
|
||
private static final String LOCK_PREFIX = "lock:"; | ||
|
||
private final RedissonClient redissonClient; | ||
private final AopForTransaction aopForTransaction; | ||
|
||
@Around("@annotation(distributedLock)") | ||
public Object lock(ProceedingJoinPoint joinPoint, DistributedLock distributedLock) throws Throwable { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
String key = | ||
LOCK_PREFIX + CustomSpringELParser.getDynamicValue(signature.getParameterNames(), joinPoint.getArgs(), | ||
distributedLock.key()); | ||
|
||
RLock rLock = redissonClient.getLock(key); | ||
|
||
try { | ||
boolean isLocked = rLock.tryLock( | ||
distributedLock.waitTime(), | ||
distributedLock.leaseTime(), | ||
distributedLock.timeUnit() | ||
); | ||
|
||
if (!isLocked) { | ||
return false; | ||
} | ||
|
||
return aopForTransaction.proceed(joinPoint); | ||
} finally { | ||
try { | ||
rLock.unlock(); | ||
} catch (Exception e) { | ||
log.info("[DistributedLock] Failed to release lock for key: {}", key, e); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
server/src/main/java/com/fluffy/global/redis/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfig { | ||
|
||
private final RedisProperties properties; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(properties.host(), properties.port()); | ||
} | ||
|
||
@Bean | ||
public RedissonClient redissonClient() { | ||
RedissonClient redisson; | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress("redis://%s:%d".formatted(properties.host(), properties.port())); | ||
redisson = Redisson.create(config); | ||
|
||
return redisson; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
server/src/main/java/com/fluffy/global/redis/RedisProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.fluffy.global.redis; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "spring.data.redis") | ||
public record RedisProperties( | ||
@NotBlank String host, | ||
@NotNull @Positive int port | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,6 @@ spring: | |
redis: | ||
host: localhost | ||
port: 6379 | ||
cache: | ||
type: redis | ||
mail: | ||
host: ${MAIL_HOST} | ||
port: 587 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,6 @@ spring: | |
redis: | ||
host: fluffy-redis | ||
port: 6379 | ||
cache: | ||
type: redis | ||
mail: | ||
host: ${MAIL_HOST} | ||
port: 587 | ||
|
Oops, something went wrong.