短视频整套源码,如何实现幂等性校验?

我们在做短视频整套源码的时候通常会遇到前端提交按钮重复点击的场景,在某些新增操作上就需要做幂等性限制来保证数据的可靠性。下面来用java aop实现幂等性校验。

一:首先我们需要一个自定义注解

 
复制代码
package com.yuku.yuku_erp.annotation;
  import java.lang.annotation.*;
  /**
  * @author 名一
  * @ClassName IdempotentAnnotation
  * @description: 用来标记需要校验幂等性的接口
  * @datetime 2024年 02月 03日 14:48
  * @version: 1.0
  */
  @Target({ElementType.METHOD})
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface IdempotentAnnotation {
  String idempotentType();
  }
复制代码

 

 

二:创建一个幂等校验的切面类

 
复制代码
package com.yuku.yuku_erp.aop;
  import com.yuku.yuku_erp.annotation.IdempotentAnnotation;
  import com.yuku.yuku_erp.constant.RedisKeyConstant;
  import com.yuku.yuku_erp.exception.MyException;
  import com.yuku.yuku_erp.utils.RedisShardedPoolUtil;
  import com.yuku.yuku_erp.utils.TokenUtil;
  import lombok.extern.slf4j.Slf4j;
  import org.aspectj.lang.JoinPoint;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Before;
  import org.aspectj.lang.annotation.Pointcut;
  import org.aspectj.lang.reflect.MethodSignature;
  import org.springframework.stereotype.Component;
  import java.lang.reflect.Method;
  /**
  * @author 名一
  * @ClassName CheckIdempotentAop
  * @description: 幂等性校验
  * @datetime 2024年 02月 03日 14:59
  * @version: 1.0
  */
  @Slf4j
  @Aspect
  @Component
  public class CheckIdempotentAop {
  @Pointcut("execution( com.yuku.yuku_erp.controller...*(..))")
  public void checkCut(){
  }
  @Before("checkCut()")
  public void checkIdempotent(JoinPoint joinPoint){
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  if (method.isAnnotationPresent(IdempotentAnnotation.class)){
  IdempotentAnnotation annotation = method.getAnnotation(IdempotentAnnotation.class);
  String idempotentType = annotation.idempotentType();
  String idempotentToken = TokenUtil.getRequest().getHeader("idempotentToken");
  String idemToken = idempotentType + idempotentToken;
  log.info("checkIdempotent idempotentType:{}, idempotentToken:{}", idempotentType, idempotentToken);
  Boolean flag = RedisShardedPoolUtil.sismember(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
  if (!flag){
  log.error("checkIdempotent error idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
  throw new MyException("该接口已提交过,请不要重复提交");
  }
  RedisShardedPoolUtil.delSetByValue(RedisKeyConstant.IDEMPOTENT_TOKEN_LIST, idemToken);
  log.info("checkIdempotent idempotentType:{}, idempotentToken:{}, flag:{}", idempotentType, idempotentToken, flag);
  }
  }
  }
复制代码

 

 

三:在需要切面的接口上使用幂等校验注解

 
@IdempotentAnnotation(idempotentType = "checkIdempotentToken")
  @GetMapping("/checkIdempotentToken")
  @ApiOperation(value = "校验幂等性示例")
  public CommonResult<String> checkIdempotentToken(){
  return CommonResult.success();
  }

 

到此幂等校验就完成了,以上就是短视频整套源码,如何实现幂等性校验?, 更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2023-11-02 直播小程序源码,react-native自定义文本输入框
2023-11-02 直播网站源码,Canvas实现圆形时间倒计时进度条
2023-11-02 在线直播系统源码,vue实现搜索文字高亮功能
2022-11-02 直播平台源代码,html网站使用js实现记住账号密码功能
2022-11-02 直播平台软件开发,java判断操作系统
2022-11-02 直播网站程序源码,搜索框实现快速搜索功能
2021-11-02 短视频系统,android Switch修改颜色修改样式滑块颜色
点击右上角即可分享
微信分享提示