Browse Source

重复提交使用@Before注解->还需要优化 本地缓存解决不了 分布式

dev
jianjun 4 years ago
parent
commit
568c600c90
  1. 19
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

19
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aop/NoRepeatSubmitAop.java

@ -6,9 +6,10 @@ import com.epmet.commons.tools.exception.RenException;
import com.google.common.cache.Cache; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
@ -32,13 +33,18 @@ public class NoRepeatSubmitAop {
*/ */
private static final Cache<String, Integer> CACHES = CacheBuilder.newBuilder() private static final Cache<String, Integer> CACHES = CacheBuilder.newBuilder()
// 最大缓存 100 个 // 最大缓存 100 个
.maximumSize(1000) .maximumSize(100)
// 设置写缓存后 5 秒钟过期 // 设置写缓存后 5 秒钟过期
.expireAfterWrite(5, TimeUnit.SECONDS) .expireAfterWrite(5, TimeUnit.SECONDS)
.build(); .build();
@Around("execution(public * com.epmet..*Controller.*(..)) && @annotation(com.epmet.commons.tools.aop.NoRepeatSubmit)") @Pointcut("@annotation(com.epmet.commons.tools.aop.NoRepeatSubmit)")
public Object around(ProceedingJoinPoint pjp) { public void doAspect() {
}
@Before("doAspect()")
public void around(JoinPoint pjp) {
try { try {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null; assert attributes != null;
@ -46,9 +52,7 @@ public class NoRepeatSubmitAop {
String key = getKey(request.getRequestURI(), pjp.getArgs()); String key = getKey(request.getRequestURI(), pjp.getArgs());
// 如果缓存中有这个url视为重复提交 // 如果缓存中有这个url视为重复提交
if (CACHES.getIfPresent(key) == null) { if (CACHES.getIfPresent(key) == null) {
Object o = pjp.proceed();
CACHES.put(key, NumConstant.ZERO); CACHES.put(key, NumConstant.ZERO);
return o;
} else { } else {
log.error("重复提交"); log.error("重复提交");
throw new RenException(EpmetErrorCode.REPEATED_SUBMIT_ERROR.getCode()); throw new RenException(EpmetErrorCode.REPEATED_SUBMIT_ERROR.getCode());
@ -59,7 +63,6 @@ public class NoRepeatSubmitAop {
log.error("验证重复提交时出现未知异常!"); log.error("验证重复提交时出现未知异常!");
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode());
} }
} }
private String getKey(String keyExpress, Object[] args) { private String getKey(String keyExpress, Object[] args) {

Loading…
Cancel
Save