#頭條創作挑戰賽#
什麼是幂等?
使用者對于同一操作發起的一次請求或者多次請求的結果是一緻的。
場景
比如添加使用者的接口,在送出時由于網絡波動或其他原因沒有及時響應,使用者可能會誤以為沒有點到送出按鈕,會再次進行送出或連續點選送出按鈕,這就會導緻同一使用者在資料庫中儲存了好幾條,這當然是不符合我們預期的,此文便可以解決這個問題,請接着看
AOP攔截
@Aspect
@Component
public class NoRepeatAspect {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Pointcut("@annotation(com.sunline.project.aop.NoRepeat) || @within(com.sunline.project.aop.NoRepeat)")
public void pointCut(){}
@Around("pointCut()")
private Object around(ProceedingJoinPoint point) {
try {
// 擷取目前使用者的token
Subject currStaff = SecurityUtils.getSubject();
UserVo user = (UserVo) currStaff.getPrincipal();
String token = user.getToken();
StaticLog.info("檢測是否重複送出");
StaticLog.info("point:{}", point);
// 擷取目前請求的方法名
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
String name = method.getName();
StaticLog.info("token:{},======methodName:{}", token, name);
if (redisTemplate.hasKey(token+name)) {
StaticLog.error("監測到重複送出>>>>>>>>>>");
return ResponseData.fail(ResponseCode.FAIL_CODE, "請勿重複送出");
}
// 擷取注解
NoRepeat annotation = method.getAnnotation(NoRepeat.class);
Long timeout = annotation.timeOut();
// 此處我用token和請求方法名為key存入redis中,有效期為timeout 時間, 也可以使用ip位址做為key
redisTemplate.opsForValue().set(token+name, token+name, timeout, TimeUnit.SECONDS);
return point.proceed();
} catch (Throwable throwable) {
StaticLog.error("=====>>>>>>操作失敗:{}", throwable);
return ResponseData.fail(ResponseCode.FAIL_CODE, "操作失敗");
}
}
}
注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoRepeat {
// 預設失效時間
long timeOut() default 10;
}
測試:@NoRepeat(timeOut = 5)
timeOut預設時間為10s
@NoRepeat(timeOut = 5)
@GetMapping(value = "/test")
@ApiOperation(value = "測試幂等注解")
@SysLogs("測試幂等注解")
@ApiImplicitParam(paramType = "header", name = "Authorization", value = "身份認證Token")
public ResponseData<T> testIdempotent() {
try {
System.err.println(new Date());
return ResponseData.ok(ResponseCode.SUCCESS_CODE, "操作成功");
} catch (Exception e) {
StaticLog.error("操作失敗:{}", e);
return ResponseData.ok(ResponseCode.FAIL_CODE, "操作失敗");
}
}