SoFunction
Updated on 2025-04-21

How to implement a Redis current limit annotation

SpringBoot implements a Redis current limit annotation

Steps to use

1.Introduce the library

  • The code is as follows (example)
        <!-- IntroducedSpringBoot Aoprely -->
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2. Code implementation

  • Add annotation
package .springai_test.annotation;

import ;
import ;
import ;
import ;

@Target()
@Retention()
public @interface RedisLimiting {

    int number() default 3;

    int time() default 60;

    String message() default "Requests are too frequent, please try again later";


}
  • Added a new current limit AOP implementation
package .springai_test.aop;

import .MD5;
import .springai_test.;
import .springai_test.;
import .springai_test.;
import .springai_test.;
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

@Aspect
@Component("redisLimitingAspect")
@Slf4j
public class RedisLimitingAspect {

    @Autowired
    private RedisUtils redisUtils;

    @Around("@annotation(.springai_test.)")  // Only intercept methods with @redisLimiting    public Object redisLimiting(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) ();
        Method method = (); // Method to directly obtain the proxy        // Get @redisLimiting annotation        RedisLimiting annotation = ();
        if (annotation == null) {
            return (); // No annotation, execute the method directly        }
        int limit = (); // Limit the number of times        int expire = ();  // Expiry time        String message = ();

        ("Intercept method: {}, Current limit key: {}, Current limit次数: {}, Expiration time: {} Second",
                (), limit, expire);

        // Execute current limiting logic        boolean isAllowed = checkRedisLimiting(method, (), limit, expire);
        if (!isAllowed) {
            throw new BusinessException(ErrorCode.BUSY_ERROR,message);
        }

        return (); // Execute the original method    }

    private boolean checkRedisLimiting(Method method, Object[] args, int limit, int expire) {
        // Generate Redis Key        String redisKey = generateRedisKey(method, args);
        // Query whether Redis exists        Object o = (redisKey);
        if (o == null) {
            (redisKey, 1, expire); // Set the initial value to 1 and set the expiration time            return true;
        } else {
            int count = (());
            if (count >= limit) {
                return false; // Exceed the limit            } else {
                (redisKey, 1); // Increment count                return true;
            }
        }
    }

    private String generateRedisKey(Method method, Object[] args) {
        StringBuilder builder = new StringBuilder();
        (().getName()).append(":").append(()).append(":");
        Parameter[] parameters = ();
        for (int i = 0; i < ; i++) {
            (parameters[i].getName()).append("=").append(args[i]).append("&");
        }
        return ().digestHex16(()); // Generate unique Redis Key    }
}
  • Implement code interception
@GetMapping("/getAllModel")
    @RedisLimiting(number = 3, time = 60,message = "Don't ask for my method to get aiModel")
    public BaseResponse<List<AiModelVO>> getAllModel() {
        return (());
    }

Summarize

The above is what we are going to talk about today. This article only briefly introduces the use of pandas, which provides a large number of functions and methods that can enable us to process data quickly and conveniently.

These are just personal experience. I hope you can give you a reference and I hope you can support me more.