当前位置:网站首页>并发幂等性防抖
并发幂等性防抖
2022-06-29 06:39:00 【墨着染霜华】

提交数据,以极快的速度点多次,相同的数据,在并发的情况,如果不做处理那么就会产生多次操作或者记录,或者主键重复报错。
为了解决这个问题,先是做了前端防抖,但是前端并不保险,能绕过或者说是网络不好的情况,
后端也是需要做防抖的处理:
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);
}
边栏推荐
- Explain canfd message and format in AUTOSAR arxml in detail
- Is virtual DOM really the fastest?
- Method of changing host name (permanent)
- 关于数据库,你应该知道的事情
- 【翻译】e-Cloud。使用KubeEdge的大规模CDN
- uva10891
- To: Hou Hong: the key to enterprise digital transformation is not technology, but strategy
- ES 查询语法
- MIPS指令集与简要分析
- Instanceklass "suggestions collection" of hotspot class model
猜你喜欢

To: Hou Hong: the key to enterprise digital transformation is not technology, but strategy

NoSQL数据库介绍

Multimodal learning pooling with context gating for video classification

微信小程序学习笔记(暑假)

反射修改final

Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2

Markdown 技能树(5):图片

机器学习笔记 - 时间序列使用机器学习进行预测

利用IPv6实现公网访问远程桌面

NoSQL数据库之Redis(四):Redis的发布和订阅
随机推荐
Introduction to QT qfileinfo
Comment choisir les fournisseurs de marques CRM dans l'industrie du vêtement?
节流的两种写法-最近看到的这种写法
Beanpostprocessor and beanfactorypostprocessor
详解Autosar Arxml中的CANFD报文及格式
Solve the problem that NPM does not have permission
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
部署Prometheus-server服务 system管理
Is virtual DOM really the fastest?
uva10635
Markdown 技能树(4):链接
What is the difference between software engineer and software development? What is the difference between software engineer and software developer?
Ci tools Jenkins installation configuration tutorial
Markdown 技能树(2):段落及强调
Two ways to write throttling - recently seen
Spark RDD案例:统计每日新增用户
CI工具Jenkins安装配置教程
Redis de la base de données nosql (v): redis Jedis Tests
Introduction to NoSQL database
Final summary spark