Skip to content

Commit

Permalink
2024-12-06 12:50:32
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Dec 6, 2024
1 parent 094fbc2 commit 7121a18
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
<modules>
<module>spring-turbo-module-configuration</module>
<module>spring-turbo-module-jackson</module>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-configuration</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jackson</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-jwt</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-misc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-misc</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion spring-turbo-module-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-redis</artifactId>
Expand All @@ -32,6 +32,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package spring.turbo.module.redis.aspect;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

/**
* 防重复调用
*
* @author 应卓
* @since 3.4.0
*/
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AvoidRepeatedInvocation {

/**
* SpringEL 表达方法调用的唯一性
*
* @return SpEL
*/
public String value();

/**
* 锁自动释放时间
*
* @return 自动释放时间
*/
public long leaseTime() default 3L;

/**
* 锁自动释放时间单位
*
* @return 自动释放时间单位
*/
public TimeUnit leaseTimeUnit() default TimeUnit.SECONDS;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package spring.turbo.module.redis.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import spring.turbo.core.AspectUtils;
import spring.turbo.core.StringSpELResolvable;
import spring.turbo.exception.RuntimeExceptionSupplier;

import java.util.Map;

/**
* @author 应卓
* @since 3.4.0
*/
@Aspect
public class AvoidRepeatedInvocationAdvice implements Ordered {

private final RedisOperations<String, String> redisOperations;
private final RuntimeExceptionSupplier exceptionSupplier;
private final int order;

/**
* 构造方法
*
* @param redisOperations RedisOperations实例,通常是 {@link StringRedisTemplate}
* @param exceptionSupplier 异常提供器,如果判断为重复调用。使用这个东西产生异常并抛出
* @param order 切面排序
*/
public AvoidRepeatedInvocationAdvice(RedisOperations<String, String> redisOperations, RuntimeExceptionSupplier exceptionSupplier, int order) {
Assert.notNull(redisOperations, "redisOperations is required");
Assert.notNull(exceptionSupplier, "exceptionSupplier is required");

this.redisOperations = redisOperations;
this.exceptionSupplier = exceptionSupplier;
this.order = order;
}

@Around("@annotation(spring.turbo.module.redis.aspect.AvoidRepeatedInvocation)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
var annotation = AspectUtils.getMethodAnnotation(joinPoint, AvoidRepeatedInvocation.class);
if (annotation == null) {
return joinPoint.proceed();
}

var method = AspectUtils.getMethod(joinPoint);

var redisKey = new StringSpELResolvable() {
@Override
public String getExpression() {
return annotation.value();
}

@Override
public Map<String, ?> getVariables() {
return Map.of(
"args", joinPoint.getArgs(),
"methodName", method.getName()
);
}
}.getValue();

var success = redisOperations.opsForValue()
.setIfAbsent(redisKey, "1", annotation.leaseTime(), annotation.leaseTimeUnit());

if (success) {
return joinPoint.proceed();
} else {
throw exceptionSupplier.get();
}
}

@Override
public int getOrder() {
return this.order;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@NonNullFields
package spring.turbo.module.redis.aspect;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
2 changes: 1 addition & 1 deletion spring-turbo-module-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webcli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webcli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-turbo-module-webmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.yingzhuo</groupId>
<artifactId>spring-turbo-modules</artifactId>
<version>3.4.0-2</version>
<version>3.4.0-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-turbo-module-webmvc</artifactId>
Expand Down

0 comments on commit 7121a18

Please sign in to comment.