日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

使用Redis實(shí)現(xiàn)接口防抖

作者:何中應(yīng) 更新時(shí)間: 2023-12-09 編程語言

說明:實(shí)際開發(fā)中,我們?cè)谇岸隧撁嫔宵c(diǎn)擊了一個(gè)按鈕,訪問了一個(gè)接口,這時(shí)因?yàn)榫W(wǎng)絡(luò)波動(dòng)或者其他原因,頁面上沒有反應(yīng),用戶可能會(huì)在短時(shí)間內(nèi)再次點(diǎn)擊一次或者用戶以為沒有點(diǎn)到,很快的又點(diǎn)了一次。導(dǎo)致短時(shí)間內(nèi)發(fā)送了兩個(gè)請(qǐng)求到后臺(tái),可能會(huì)導(dǎo)致數(shù)據(jù)重復(fù)添加。

為了避免短時(shí)間內(nèi)對(duì)一個(gè)接口訪問,我們可以通過AOP+自定義注解+Redis的方式,在接口上加一個(gè)自定義注解,然后通過AOP的前置通知,在Redis中存入一個(gè)有效期的值,當(dāng)訪問接口時(shí)這個(gè)值還未過期,則拋出異常,以此來避免短時(shí)間內(nèi)對(duì)接口的方法。

實(shí)現(xiàn)

第一步:創(chuàng)建一個(gè)自定義注解,設(shè)置兩個(gè)屬性,一個(gè)是key,一個(gè)是這個(gè)key在Redis中的有效時(shí)間;

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();

    /**
     * 限制訪問時(shí)間
     * @return
     */
    int times();
}

第二步:創(chuàng)建對(duì)應(yīng)的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 {
        // 獲取切入點(diǎn)上面的自定義注解
        Signature signature = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature) signature;

        // 獲取方法上面的注解
        LimitAccess limitAccess = methodSignature.getMethod().getAnnotation(LimitAccess.class);

        // 獲取注解上面的屬性
        int limit = limitAccess.times();
        String key = limitAccess.key();

        // 根據(jù)key去找Redis中的值
        Object o = redisTemplate.opsForValue().get(key);

        // 如果不存在,說明是首次訪問,存入Redis,過期時(shí)間為limitAccess中的time
        if (o == null) {
            redisTemplate.opsForValue().set(key, "", limit, TimeUnit.SECONDS);
            // 執(zhí)行切入點(diǎn)的方法
            return pjp.proceed();
        } else {
            // 如果存在,說明不是首次訪問,拋出異常
            throw new RuntimeException("訪問過于頻繁");
        }
    }
}

第三步:在需要限制的接口上,加上這個(gè)注解,并設(shè)置key和限制訪問時(shí)間,如下這個(gè)這個(gè)接口,設(shè)置key為set,實(shí)際開發(fā)中可以設(shè)置一個(gè)隨機(jī)值,或者按照規(guī)則自定義設(shè)置,times為限制訪問時(shí)間;

    @GetMapping("/limit")
    @LimitAccess(key = "limit", times = 10)
    public String limit() {
        return "success";
    }

演示

啟動(dòng)項(xiàng)目,訪問該接口;

(首次訪問,沒得問題,同時(shí)在Redis中存入值)

在這里插入圖片描述


(短時(shí)間內(nèi),連續(xù)訪問,因?yàn)镽edis中值未過期)

在這里插入圖片描述

在這里插入圖片描述


(10秒之后再訪問,又可以了,Redis中的值過期了)

在這里插入圖片描述

以上就是使用Redis實(shí)現(xiàn)接口防抖,避免短時(shí)間內(nèi)連續(xù)訪問接口。實(shí)際開發(fā)中,可以將異常設(shè)置為自定義異常。

原文鏈接:https://blog.csdn.net/qq_42108331/article/details/134691925

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新