-
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.
- Loading branch information
Showing
12 changed files
with
141 additions
and
9 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
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
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
39 changes: 39 additions & 0 deletions
39
...-module-redis/src/main/java/spring/turbo/module/redis/aspect/AvoidRepeatedInvocation.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,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; | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...e-redis/src/main/java/spring/turbo/module/redis/aspect/AvoidRepeatedInvocationAdvice.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,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; | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
spring-turbo-module-redis/src/main/java/spring/turbo/module/redis/aspect/package-info.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,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.redis.aspect; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
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