Commit 5bbff7b3 by 吕明尚

修改验券幂等

parent a35eee80
......@@ -177,11 +177,6 @@
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package share.common.utils;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Objects;
/**
* @ClassName RedisLockUtil
* @Description 使用redis做锁
* @Author Wangyujie
* @Version V1.1.0
*/
@Component
public class RedisLockUtil {
@Resource
RedisTemplate<String, Object> redisTemplate;
/**
* 获取锁Key
*
* @param prefix 前缀
* @param name 名称
* @return
*/
public static String getFullKey(String prefix, String name) {
return prefix + "_" + name;
}
/**
* 获取锁,true 则得到锁,false 已被锁定
*
* @param lockName 锁名称
* @param lockExoire 锁时间毫秒
* @return
*/
public Boolean getLock(String lockName, Integer lockExoire) {
return (Boolean) redisTemplate.execute((RedisCallback<?>) connection -> {
// 获取时间毫秒值
long expireAt = System.currentTimeMillis() + lockExoire + 1;
// 获取锁
Boolean acquire = connection.setNX(lockName.getBytes(), String.valueOf(expireAt).getBytes());
if (acquire) {
return true;
} else {
byte[] bytes = connection.get(lockName.getBytes());
// 非空判断
if (Objects.nonNull(bytes) && bytes.length > 0) {
long expireTime = Long.parseLong(new String(bytes));
// 如果锁已经过期
if (expireTime < System.currentTimeMillis()) {
// 重新加锁,防止死锁
byte[] set = connection.getSet(lockName.getBytes(),
String.valueOf(System.currentTimeMillis() + lockExoire + 1).getBytes());
return Long.parseLong(new String(set)) < System.currentTimeMillis();
}
}
}
return false;
});
}
/**
* 删除锁
*
* @param lockName
*/
public void delLock(String lockName) {
redisTemplate.delete(lockName);
}
}
package share.common.utils.aop;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RepeatSubmit {
String key() default "";
}
package share.common.utils.aop;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
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.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Aspect
@Component
public class RepeatSubmitAspect {
private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
// 最大缓存 设置为1000个
.maximumSize(1000)
// 设置写缓存后1s过期
.expireAfterWrite(1, TimeUnit.SECONDS)
.build();
@Around("execution(public * *(..)) && @annotation(share.common.utils.aop.RepeatSubmit))")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
RepeatSubmit repeatSubmit = method.getAnnotation(RepeatSubmit.class);
String key = getKey(repeatSubmit.key(), pjp.getArgs());
if (StringUtils.isNotBlank(key)) {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("请勿重复请求");
}
// 如果是第一次请求,就将key 当前对象压入缓存中
CACHES.put(key, key);
}
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException("服务器异常!");
}
}
/**
* key 的生成策略
*
* @param keyExpress 表达式
* @param args 参数
* @return 生成的key
*/
private String getKey(String keyExpress, Object[] args) {
for (int i = 0; i < args.length; i++) {
keyExpress = keyExpress.replace("arg[" + i + "]", args[i].toString());
}
return keyExpress;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment