当前位置:网站首页>Concurrent idempotent anti shake
Concurrent idempotent anti shake
2022-06-29 07:36:00 【Inking and frosting】

Submit data , Click many times at a very fast speed , Same data , In the case of concurrency , If no processing is done, multiple operations or records will be generated , Or the primary key repeatedly reports an error .
To solve this problem , First, the front-end anti shake is made , But the front end is not safe , Can bypass or say the network is bad ,
The back end also needs to be anti shake :
1、 Cut around 、 locked 、 Idempotent function
@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(" To request :{}",key);
if ( !redisService.haskey(key) ) {// If there is this in the cache url It is deemed to be a duplicate submission
Object o = pjp.proceed();
redisService.setCacheObject(key, 0, 10, TimeUnit.SECONDS);
// log.info(" Normal to submit :{},{}",key, MyDateUtil.getNowDateTime());
return o;
} else {
redisService.setCacheObject(key, 0, 10, TimeUnit.SECONDS);// Ordered the same URL Continue restriction , until 2 The interval between clicks exceeds the limit
//return R.error(-889," Do not submit repeatedly or operate too frequently !");
// log.info(" Limit submission :{},{}",key, MyDateUtil.getNowDateTime());
throw new BusinessException(" Do not submit repeatedly or operate too frequently !( interval 10 second , Recalculate each click )");
}
}
}2、 Annotations use
@Target(ElementType.METHOD) // It works on the method
@Retention(RetentionPolicy.RUNTIME) // Run time effective
public @interface NoRepeatSubmit {
String name() default "name:";
}
@NoRepeatSubmit
@PostMapping("/goods/save")
@ApiOperation(value = " Mall goods - preservation ", notes = " This feature adds 、 It can be modified , Single processing , Multiple processes are OK ")
@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);
}
边栏推荐
- 什么是测试架构师
- 阿里云访问资源:NoSuchKey
- Kingbasees v8r6 cluster maintenance case - data migration between clusters
- Final summary spark
- Relevance - correlation analysis
- Markdown skill tree (3): title
- 358. K 距离间隔重排字符串 排序
- Listen to textarea input through Keyup to change button style
- 节流的两种写法-最近看到的这种写法
- What you should know about databases
猜你喜欢

1183:病人排队

关于数据库,你应该知道的事情

A hybrid model of machine learning notes time series

帆船动力学仿真分析

【翻译】e-Cloud。使用KubeEdge的大规模CDN

Appium automation test foundation ADB common commands (II)
![[popular science materials] materials from scientific spirit to scientific knowledge](/img/fc/647805056c8ff2b30e894a6e6c4a36.jpg)
[popular science materials] materials from scientific spirit to scientific knowledge

部署Prometheus-server服务 system管理

Using IPv6 to access remote desktop through public network

阿里云访问资源:NoSuchKey
随机推荐
ES中配置ext.dic文件不生效的原因
Matlab Simulink simulation and analysis of power grid sweep frequency
Check whether tensorflow supports GPU and test program
Explain canfd message and format in AUTOSAR arxml in detail
tf.compat.v1.global_variables
systemd 管理node-exporter
excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例
Select distinct on statement in kingbasees
Appium自动化测试基础 — ADB常用命令(三)
Markdown 技能树(8):代码块
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
【翻译】簇拥而出。构建现代应用程序的设计方法
Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
Appium automation test foundation ADB common commands (II)
tf.compat.v1.assign
Webrtc series - 8-connectivity detection for network transmission
shell条件判断详解
tf. compat. v1.assign
KingbaseES V8R6集群维护案例之--单实例数据迁移到集群案例
项目中 if else 的代替写法