PHPackages                             cjphp/rate-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. cjphp/rate-limiter

ActiveLibrary

cjphp/rate-limiter
==================

php限流器

v1.0.0(2y ago)08MITPHPPHP &gt;=8.1

Since May 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/hn-chenji/rate-limiter)[ Packagist](https://packagist.org/packages/cjphp/rate-limiter)[ RSS](/packages/cjphp-rate-limiter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

API限流器
======

[](#api限流器)

该限流器采用php技术开发，支持以下特性
--------------------

[](#该限流器采用php技术开发支持以下特性)

### 存储方式

[](#存储方式)

- 文件存储
- redis存储

### 流控策略

[](#流控策略)

- 固定窗口策略
- 滑动窗口策略
- 漏桶策略

项目结构
----

[](#项目结构)

```
rate-limiter/
├── src/
│   ├── RateLimiter.php                     限流器类
│   ├── StorageFactory.php                  存储工厂类
│   ├── StrategyFactory.php                 策略工厂类
│   ├── Storage/
│   │   ├── StorageInterface.php            存储接口
│   │   ├── FileStorage.php                 文件存储实现类
│   │   └── RedisStorage.php                redis存储实现类
│   └── Strategy/
│       ├── StrategyInterface.php           策略接口
│       ├── FixedWindowStrategy.php         固定窗口策略类
│       ├── SlidingWindowStrategy.php       滑动窗口策略类
│       └── LeakyBucketStrategy.php         漏桶策略类
├── composer.json
└── README.md

```

使用方法
----

[](#使用方法)

1. 添加依赖

```
composer require cjphp/rate-limiter:~1.0

```

2. 在项目需要限流的地方初始化 RateLimiter 类，并调用 isRateLimited 方法，示例如下

```
    public function needRateLimitFunction()
    {
        ...

        $key = 'user_123'; //key 为特定对象，此处假设用户id
        //以下可以写入配置文件 动态读取
        $storeType = "file";  //可选：file|redis
        $strategyType = "fixed_window"; //可选：fixed_window|sliding_window|leaky_bucket
        $limit = 10; // 每60秒 10 次请求
        $window = 60; // 60 秒
        $rate = 1; // 每秒漏5滴
        $capacity = 3; // 桶容量10
        $isGlobal = true; //是否全局限流
        $params = ["limit"=>$limit, "window"=> $window, "capacity"=>$capacity, "rate"=>$rate];

        $ext = match ($storeType) {
            'file' => '文件存储路径...',
            'redis' => redis实例,
        };
        $storage = StorageFactory::create($storeType, $ext);
        $strategy = StrategyFactory::create($strategyType, $storage, $params);

        $rateLimiter = new RateLimiter($storage, $strategy);

        if ($rateLimiter->isRateLimited($key, $isGlobal)) {
            echo "不好意思，您被流控了";
        } else {
            echo "允许通行.";
        }

        ...
    }

```

###  Health Score

22

↓

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

731d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7bf6dc3c44bc3de3b601305e1bcc83ca51bb677ca67b0277725edae6e7d40ca5?d=identicon)[chenji](/maintainers/chenji)

---

Top Contributors

[![ilookcc](https://avatars.githubusercontent.com/u/1941688?v=4)](https://github.com/ilookcc "ilookcc (1 commits)")

### Embed Badge

![Health badge](/badges/cjphp-rate-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/cjphp-rate-limiter/health.svg)](https://phpackages.com/packages/cjphp-rate-limiter)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
