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 @ 2023-05-04 17:28  杨吃羊  阅读(138)  评论(0)    收藏  举报