说明:实际开发中,我们在前端页面上点击了一个按钮,访问了一个接口,这时因为网络波动或者其他原因,页面上没有反应,用户可能会在短时间内再次点击一次或者用户以为没有点到,很快的又点了一次。导致短时间内发送了两个请求到后台,可能会导致数据重复添加。
为了避免短时间内对一个接口访问,我们可以通过aop+自定义注解+redis的方式,在接口上加一个自定义注解,然后通过aop的前置通知,在redis中存入一个有效期的值,当访问接口时这个值还未过期,则抛出异常,以此来避免短时间内对接口的方法。
实现
第一步:创建一个自定义注解,设置两个属性,一个是key,一个是这个key在redis中的有效时间;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
/**
* 自定义注解
*/
@target(elementtype.method)
@retention(retentionpolicy.runtime)
public @interface limitaccess {
/**
* 限制访问的key
* @return
*/
string key();
/**
* 限制访问时间
* @return
*/
int times();
}
第二步:创建对应的aspect;
import com.hezy.annotation.limitaccess;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.signature;
import org.aspectj.lang.annotation.around;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.pointcut;
import org.aspectj.lang.reflect.methodsignature;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.stereotype.component;
import java.util.concurrent.timeunit;
/**
* aop类(通知类)
*/
@component
@aspect
public class limitaspect {
@autowired
private redistemplate redistemplate;
@pointcut("@annotation(com.hezy.annotation.limitaccess)")
public void pt(){};
@around("pt()")
public object aoparound(proceedingjoinpoint pjp) throws throwable {
// 获取切入点上面的自定义注解
signature signature = pjp.getsignature();
methodsignature methodsignature = (methodsignature) signature;
// 获取方法上面的注解
limitaccess limitaccess = methodsignature.getmethod().getannotation(limitaccess.class);
// 获取注解上面的属性
int limit = limitaccess.times();
string key = limitaccess.key();
// 根据key去找redis中的值
object o = redistemplate.opsforvalue().get(key);
// 如果不存在,说明是首次访问,存入redis,过期时间为limitaccess中的time
if (o == null) {
redistemplate.opsforvalue().set(key, "", limit, timeunit.seconds);
// 执行切入点的方法
return pjp.proceed();
} else {
// 如果存在,说明不是首次访问,抛出异常
throw new runtimeexception("访问过于频繁");
}
}
}
第三步:在需要限制的接口上,加上这个注解,并设置key和限制访问时间,如下这个这个接口,设置key为limit,实际开发中可以设置一个随机值,或者按照规则自定义设置,times为限制访问时间;
@getmapping("/limit")
@limitaccess(key = "limit", times = 10)
public string limit() {
return "success";
}
演示
启动项目,访问该接口;
(首次访问,没得问题,同时在redis中存入值)

(短时间内,连续访问,因为redis中值未过期)


(10秒之后再访问,又可以了,redis中的值过期了)

以上就是使用redis实现接口防抖,避免短时间内连续访问接口。实际开发中,可以将异常设置为自定义异常。
到此这篇关于redis实现接口防抖的示例代码的文章就介绍到这了,更多相关redis 接口防抖内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论