短视频整套源码,如何实现幂等性校验?
我们在做短视频整套源码的时候通常会遇到前端提交按钮重复点击的场景,在某些新增操作上就需要做幂等性限制来保证数据的可靠性。下面来用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(); }
到此幂等校验就完成了,以上就是短视频整套源码,如何实现幂等性校验?, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 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修改颜色修改样式滑块颜色