并发幂等性防抖

提交数据,以极快的速度点多次,相同的数据,在并发的情况,如果不做处理那么就会产生多次操作或者记录,或者主键重复报错。

为了解决这个问题,先是做了前端防抖,但是前端并不保险,能绕过或者说是网络不好的情况,

后端也是需要做防抖的处理:

1、做切面环绕、上锁、幂等函数

@Slf4j
@Aspect
@Component
public class NoRepeatSubmitAop {
 

    @Autowired
    private RedisService redisService;

//                         com.hieasy.icrm.project.weixin.controller.wxma.miniapp
    @Synchronized
    @Around("execution(* com.hieasy.icrm.project.weixin.controller.*.*.*Ctrl.*(..)) && @annotation(nrs)")
    public Object arround(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) throws Throwable {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String token=request.getHeader("token");
            if(MyStrUtils.isEmpty(token)) return pjp.proceed();
            String key = token+ "-" + request.getServletPath();
//            log.info("开始请求:{}",key);
            if ( !redisService.haskey(key) ) {// 如果缓存中有这个url视为重复提交
                Object o = pjp.proceed();
                redisService.setCacheObject(key, 0, 10, TimeUnit.SECONDS);
//                log.info("正常提交:{},{}",key, MyDateUtil.getNowDateTime());
                return o;
            } else {
                redisService.setCacheObject(key, 0, 10, TimeUnit.SECONDS);//点了同样的URL继续限制,直到2次点击中间间隔超过了限制
                //return R.error(-889,"请勿重复提交或者操作过于频繁!");
//                log.info("限制提交:{},{}",key, MyDateUtil.getNowDateTime());
                throw new BusinessException("请勿重复提交或者操作过于频繁!(间隔10秒,每次点击重新计算)");
            }
    }
 
}

2、注解使用

    @Target(ElementType.METHOD) // 作用到方法上
    @Retention(RetentionPolicy.RUNTIME) // 运行时有效
    public @interface NoRepeatSubmit {
        String name() default "name:";
    }


    @NoRepeatSubmit
    @PostMapping("/goods/save")
    @ApiOperation(value = "商城商品-保存", notes = "此功能新增、修改均可,单处理,多条处理也都可以")
    @ApiOperationSupport(includeParameters = {})
    public JsonResult save(@RequestBody List<CrmWxMallGoods> list) throws Exception {
        Long tenantId=getPresentTenantId();
        int rows=0;
        QueryWrapper wrapper;
        CrmWxMallGoods item;
        for(CrmWxMallGoods goods:list){
            goods.setTenantId(tenantId);
            wrapper=new QueryWrapper();
            wrapper.eq("tenant_id", tenantId);
            wrapper.eq("item_code", goods.getItemCode());
            item= crmWxMallGoodsService.getOne(wrapper);
            if(null!=item){
                goods.setId(item.getId());
                rows+=crmWxMallGoodsService.updateById(goods)?1:0;
            }else{
                rows+=crmWxMallGoodsService.save(goods)?1:0;
            }
        }
        return toJRAjax(rows);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值