避免重复提交注解
避免重复提交注解
①注解
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AvoidRepeatableCommit { /** * 指定时间内不可重复提交,单位毫秒 * @return */ long timeout() default 100; }
② 实现方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | @Aspect @Component @Slf4j public class AvoidRepeatableCommitAspect { @Resource private RedisService redisService; /** * @param point */ @Around( "@annotation(com.iktapp.skc.common.security.aspect.avoidrepeat.AvoidRepeatableCommit)" ) public Object around(ProceedingJoinPoint point) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); String ip = IPUtil.getIPAddress(request); //获取注解 MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); //目标类、方法 String className = method.getDeclaringClass().getName(); String name = method.getName(); String ipKey = String.format( "%s#%s" ,className,name); int hashCode = Math.abs(ipKey.hashCode()); String key = String.format( "%s_%d" ,ip,hashCode); AvoidRepeatableCommit avoidRepeatableCommit = method.getAnnotation(AvoidRepeatableCommit. class ); long timeout = avoidRepeatableCommit.timeout(); if (timeout < 0){ //过期时间5秒 timeout = 5*1000; } String value = (String) redisService.getCacheObject(key); if (StringUtils.isNotBlank(value)){ return AjaxResult.error( "请勿重复提交" ); } redisService.setCacheObject(key, UUID.randomUUID().toString(),timeout, TimeUnit.MILLISECONDS); //执行方法 Object object = point.proceed(); return object ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?