您现在的位置是:主页 > news > 做网站 什么语言/百度竞价效果怎么样
做网站 什么语言/百度竞价效果怎么样
admin2025/5/13 23:31:22【news】
简介做网站 什么语言,百度竞价效果怎么样,建立网站要钱吗?,老鬼seo防止表单重复提交,重复请求。程序幂等性预防 需要用到Redis防止表单重复提交,重复请求。程序幂等性预防防止表单重复提交,重复请求。程序幂等性预防自定义注解:AOP切面类:自定义注解调用实例:自定义注解&a…
做网站 什么语言,百度竞价效果怎么样,建立网站要钱吗?,老鬼seo防止表单重复提交,重复请求。程序幂等性预防
需要用到Redis防止表单重复提交,重复请求。程序幂等性预防防止表单重复提交,重复请求。程序幂等性预防自定义注解:AOP切面类:自定义注解调用实例:自定义注解&a…
防止表单重复提交,重复请求。程序幂等性预防
需要用到Redis
防止表单重复提交,重复请求。程序幂等性预防
- 防止表单重复提交,重复请求。程序幂等性预防
- 自定义注解:
- AOP切面类:
- 自定义注解调用实例:
自定义注解:
作用于方法上,打了此注解的方法会通过Spring AOP进行幂等性检查:
/*** 请求幂等性检查*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CheckIdempotency {//模块描述String model() default "";//锁失效时间int timeOut() default 10;//异常信息String ex() default "程序正在执行,请勿重复请求";//是否手动删除锁boolean delLock() default true;
}
AOP切面类:
对CheckIdempotency 自定义注解进行AOP操作,通过Redis的setNX分布式锁的特性进行幂等性检查:
@Aspect
@Component
public class CheckIdempotencyAop {@AutowiredRedisTemplate redisTemplate;@Pointcut("@annotation(com.qzsoft.common.annotation.CheckIdempotency)")private void method() {}@Around(value = "method()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable {Signature signature = pjp.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method targetMethod = methodSignature.getMethod();CheckIdempotency checkIdempotency = targetMethod.getAnnotation(CheckIdempotency.class);String model = checkIdempotency.model();int timeOut = checkIdempotency.timeOut();String ex = checkIdempotency.ex();Object[] values = pjp.getArgs();StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < values.length; i++) {stringBuffer.append(values[i]);}boolean success = setNx(stringBuffer+model,"java",timeOut,TimeUnit.SECONDS);if(!success){throw BusinessException.buildBiz(ex);}pjp.proceed(values);if(checkIdempotency.delLock())redisTemplate.delete(stringBuffer+model);return true;}@AfterThrowing(value = "method()", throwing="throwinfo")public void afterThrowing(JoinPoint pjp, Throwable throwinfo) {Signature signature = pjp.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method targetMethod = methodSignature.getMethod();CheckIdempotency checkIdempotency = targetMethod.getAnnotation(CheckIdempotency.class);String model = checkIdempotency.model();String ex = checkIdempotency.ex();Object[] values = pjp.getArgs();StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < values.length; i++) {stringBuffer.append(values[i]);}if(!ex.equals(throwinfo.getMessage()))//如果程序逻辑报错,则删除锁。 如果错误信息是自定义的幂等性提示信息,说明出现了幂等性问题(多并发,或多次请求)则不删除锁。redisTemplate.delete(stringBuffer+model);}private boolean setNx(String key, String value, long expires, TimeUnit timeUnit) {boolean flag = false;try {flag = (boolean) redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.set(key.getBytes(), value.getBytes(), Expiration.from(expires, timeUnit), RedisStringCommands.SetOption.ifAbsent()));} catch (Exception e) {e.printStackTrace();}return flag;}
}
自定义注解调用实例:
在需要幂等性检查的方法上标注自定义注解:
@CheckIdempotency(model = CREATESAMPMODEL,timeOut = 35,ex = EX,delLock = false)public boolean createSamples(String projId){........}