Detailed explanation of PHP's list-type data structure based on redis to implement IP current limiting operation

藏色散人
Release: 2023-04-09 18:22:01
forward
3283 people have browsed it

Recommended: "PHP Video Tutorial"

In daily business function development, if you want torestrict any IP to only access within a certain period of time How to implement a certain interface a certain number of times?
This functional requirement is usually used to prevent scripts from maliciously brushing interfaces. Currently, there are many relatively complete current limiting solutions on the Internet. For general sites, you can use the linked list data structure of redis to implement the IP current limiting function.
For example -
If we need to implement, for interface A, limit any IP to allow up to 3 accesses in each consecutive 5 seconds, and return an error if more than 3 times.
Detailed explanation of PHPs list-type data structure based on redis to implement IP current limiting operation
For the above picture, at 08 seconds, 4 requests have been initiated in the last 5 seconds, which has reached the maximum number limit, so access will be restricted at this time.
If implemented in PHP, the specific logic code is as follows -

/**
* 检查队列的长度是否到达设定的阈值,已到达则返回false,未到达则将当前时间戳推入队列最末端,同时刷新队列整体的缓存时间
* @param $key 队列缓存的key
* @param $expire 队列缓存过期时间,例如上面例子中的5秒
* @param $limit 队列长度阈值,如上面例子中的3次
* @return bool
*/public function checkLimit($key, $expire, $limit){
    $length = $this->refreshList($key, $expire);
    if ($length rPush($key, time());
        $this->expire($key, intval($limit));
        return true;
    }
    return false;}/** 
* 刷新队列,过滤掉已经不在有效时间内的值,返回最新队列的长度
* @param $key string 自定义的缓存key
* @param $expire 队列缓存过期时间,例如上面例子中的5秒
* @return bool|int
*/public function refreshList ($key, $expire)   
 {
        if ($this->has($key)) {
            do { // 对于已存在数据的list,要先从前往后把已经过期的数据弹出
                $oldest_value = $this->lPop($key);
            } while ($oldest_value && time() - $oldest_value > $expire);
            // 把最后弹出的数据重新塞回list的最前边
            $oldest_value && $this->lPush($key, $oldest_value);
            return $this->lLen($key);
        }
        return 0;}
Copy after login

The lPop, lPush, lLen, rPush and other methods used are all encapsulated after redis expansion , some methods of operating linked list data structures, parameters and return values ​​are consistent with native methods.
In fact, I later found out after checking online that redis handles this scenario more directly by using an ordered set like zset. The logic is basically the same, that is, it stores the current timestamp and then uses the sliding window algorithm. The idea is to determine whether the value length in the current window has exceeded the limit.

The above is the detailed content of Detailed explanation of PHP's list-type data structure based on redis to implement IP current limiting operation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!