redis+自定义注解+AOP实现接口幂等性防重复提交

 

1 自定义注解接口:

复制代码
import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {
    //防重复操作过期时间默认1s
    long expireTime() default 1;
}
复制代码

2 给注解编写切面

 

复制代码
package kun.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
@Slf4j
@Component
@Aspect
public class RepeatSubmitAspect {
    @Autowired
    private RedisTemplate redisTemplate;
    //定义切点
    @Pointcut("@annotation(kun.redis.RepeatSubmit)")
    public void repeatSubmit(){}
    //环绕通知
    @Around("repeatSubmit()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
//1 根据url+use_id+token生成redis的key
//2 如果当前redis中没有这个key就存入redis并设置过期时间并且放行
//3 如果当前redis中已经存在这个key就返回"请勿重复提交"
}
}
复制代码

 

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