atakde/php-rate-limiter

该软件包的最新版本(v1.0.0)没有可用的许可信息。

PHP速率限制器

v1.0.0 2022-12-07 17:46 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:19:28 UTC


README

一个用于速率限制的PHP包。

安装

通过composer安装

composer require atakde/php-rate-limiter

使用方法

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$storage = new RedisStorage($redis);

// Allow maximum 10 requests in 60 seconds.
$rateLimitter = new RateLimiter([
    'refillPeriod' => 60,
    'maxCapacity' => 10,
    'prefix' => 'api'
], $storage);

$ip = $_SERVER['REMOTE_ADDR'];
if ($rateLimitter->check($ip)) {
    echo 'OK';
} else {
    echo 'Limit Exceeded';
}