避免重复提交注解

避免重复提交注解

 

①注解

复制代码
@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;
    }
 
}

  

 

posted @   xingmeng1  阅读(94)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示