agussuroyo/rate-limiter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

agussuroyo/rate-limiter

最新稳定版本:1.0.0

Composer 安装命令:

composer require agussuroyo/rate-limiter

包简介

A simple and flexible rate limiter for PHP

README 文档

README

RateLimiter is a simple and flexible PHP package for rate limiting requests using different storage backends such as file storage and Redis.

Features

  • Set request limits dynamically.
  • Supports multiple storage backends (File, Redis).
  • Easy-to-use API.
  • Complies with SOLID principles for extensibility.

Installation

You can install the package via Composer:

composer require agussuroyo/rate-limiter

Usage

Basic Usage

use AgusSuroyo\RateLimiter\RateLimiter;

$limiter = new RateLimiter('guest', 5, 60); // Key, Max Attempts, Decay Seconds

if ($limiter->tooManyAttempts()) {
    echo "Too many requests. Please try again later.";
    return;
}

$limiter->hit();
echo "Request allowed.";

Constructor Parameters

The RateLimiter constructor accepts four parameters:

  1. Key (string) - A unique identifier for the rate limit (e.g., user ID or IP address).
  2. Max Attempts (int) - The maximum number of allowed requests within the decay period.
  3. Decay Seconds (int) - The time window (in seconds) before the attempt count resets.
  4. Storage (StorageInterface, optional) - The storage mechanism to persist request attempts. If not provided, it defaults to FileStorage.

Using Redis Storage

use AgusSuroyo\RateLimiter\RateLimiter;
use AgusSuroyo\RateLimiter\Storage\RedisStorage;

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

$limiter = new RateLimiter('guest', 5, 60, $storage);

if ($limiter->tooManyAttempts()) {
    echo "Too many requests. Please try again later.";
    return;
}

$limiter->hit();
echo "Request allowed.";

Clearing Rate Limit

$limiter->clear();

Creating a Custom Storage

To implement a custom storage, create a class that implements StorageInterface:

use AgusSuroyo\RateLimiter\Contracts\StorageInterface;

class CustomStorage implements StorageInterface
{
    private array $data = [];

    public function getAttempts(string $key): array
    {
        return $this->data[$key] ?? [];
    }

    public function saveAttempts(string $key, array $attempts, int $decaySeconds): void
    {
        $this->data[$key] = $attempts;
    }

    public function clear(string $key): void
    {
        unset($this->data[$key]);
    }
}

$customStorage = new CustomStorage();
$limiter = new RateLimiter('guest', 5, 60, $customStorage);

Testing

Run unit tests using PHPUnit:

vendor/bin/phpunit

Contributing

Contributions are welcome! Please submit a pull request or open an issue.

License

This package is open-source software licensed under the MIT license.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-02