PHPackages                             lingyun/think-rate-limiting - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lingyun/think-rate-limiting

ActiveThink-extend[Utility &amp; Helpers](/categories/utility)

lingyun/think-rate-limiting
===========================

基于 ThinkPHP 缓存实现的限流器，提供了一个简单的方法来限制指定时间内的任何操作

v1.0.1(2y ago)017Apache-2.0PHP

Since Sep 5Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ouyanglingyun/think-rate-limiting)[ Packagist](https://packagist.org/packages/lingyun/think-rate-limiting)[ RSS](/packages/lingyun-think-rate-limiting/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

think-RateLimiter
=================

[](#think-ratelimiter)

基于 ThinkPHP 缓存实现的限流器，提供了一个简单的方法来限制指定时间内的任何操作。

- [安装](#installation)
- [缓存配置](#config)
- [基础用法](#basicUsage)
    - [手动配置尝试次数](#manuallyIncrementingAttempts)
    - [确定限流器可用性](#determineTheAvailabilityOfCurrentLimiters)
    - [清除尝试次数](#clearingAttempts)

安装
--

[](#安装)

通过 Composer 包管理器安装 **think-think-rate-limiting** :

```
composer require lingyun/think-rate-limiting

```

缓存配置
----

[](#缓存配置)

通常情况下，限流器使用你默认的缓存驱动，由 `cache` 配置文件中的 `default` 键定义。你也可以通过在你的应用程序的 `cache` 配置文件中定义一个 `limiter` 来指定限流器应该使用哪一个缓存来驱动：

```
return [
    'default'    =>    'file',
    'stores'    =>    [
        // 文件缓存
        'file'   =>  [
            // 驱动方式
            'type'   => 'file',
            // 设置不同的缓存保存目录
            'path'   => '../runtime/file/',
        ],
        // redis缓存
        'redis'   =>  [
            // 驱动方式
            'type'   => 'redis',
            // 服务器地址
            'host'       => '127.0.0.1',
        ],
    ],
    'limiter' => 'redis',
];

```

基础用法
----

[](#基础用法)

可以通过 `think\facade\RateLimiter` 来操作限流器。限流器提供的最简单的方法是 `attempt` 方法，它将一个给定的回调函数执行次数限制在一个给定的秒数内。

当回调函数执行次数超过限制时， `attempt` 方法返回 `false` ；否则 `attempt` 方法将返回回调的结果或 `true` 。 `attempt` 方法接受的第一个参数是一个速率限制器 `key` ，它可以是你选择的任何字符串，代表被限制速率的动作：

```
use think\facade\RateLimiter;

$executed = RateLimiter::attempt(
    'send-message:'.$user->id,
    $perMinute = 5,
    function() {
        // 发送消息...
    }
);

if (! $executed) {
    return 'Too many messages sent!';
}

```

### 手动配置尝试次数

[](#手动配置尝试次数)

如果想手动与限流器交互，可以使用多种方法。例如，可以调用 `tooManyAttempts` 方法来确定给定的限流器是否超过了每分钟允许的最大尝试次数

```
use think\facade\RateLimiter;

if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {
    return 'Too many attempts!';
}

```

或者，可以使用 `remaining` 方法检索给定密钥的剩余尝试次数。如果给定的密钥还有重试次数，可以调用 `hit` 方法来增加总尝试次数:

```
use think\facade\RateLimiter;

if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) {
    RateLimiter::hit('send-message:'.$user->id);

    // 发送消息。。。
}

```

### 确定限流器可用性

[](#确定限流器可用性)

当一个键没有更多的尝试次数时，`availableIn` 方法返回在更多尝试可用之前剩余的秒数：

```
use think\facade\RateLimiter;

if (RateLimiter::tooManyAttempts('send-message:'.$user->id, $perMinute = 5)) {
    $seconds = RateLimiter::availableIn('send-message:'.$user->id);

    return 'You may try again in '.$seconds.' seconds.';
}

```

### 清除尝试次数

[](#清除尝试次数)

可以使用 `clear` 方法重置给定速率限制键的尝试次数。例如，当接收者读取给定消息时，可以重置尝试次数:

```
use app\model\Message;
use think\facade\RateLimiter;

/**
 * 将邮件标记为已读。
 *
 * @param  \app\model\Message  $message
 * @return \app\model\Message
 */
public function read(Message $message)
{
    $message->markAsRead();

    RateLimiter::clear('send-message:'.$message->user_id);

    return $message;
}

```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

Every ~19 days

Total

2

Last Release

960d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a281e6786586cdc667ad579d325f56251f4ac8b7e698c5ec34779db5f1ed7ef6?d=identicon)[lingyun](/maintainers/lingyun)

---

Top Contributors

[![ouyanglingyun](https://avatars.githubusercontent.com/u/23470603?v=4)](https://github.com/ouyanglingyun "ouyanglingyun (9 commits)")

### Embed Badge

![Health badge](/badges/lingyun-think-rate-limiting/health.svg)

```
[![Health](https://phpackages.com/badges/lingyun-think-rate-limiting/health.svg)](https://phpackages.com/packages/lingyun-think-rate-limiting)
```

###  Alternatives

[topthink/think-captcha

captcha package for thinkphp

132934.4k68](/packages/topthink-think-captcha)[topthink/think-worker

workerman extend for thinkphp

202227.2k9](/packages/topthink-think-worker)[yunwuxin/think-cron

计划任务

17416.2k](/packages/yunwuxin-think-cron)[zzstudio/think-addons

The ThinkPHP6 Addons Package

1518.6k](/packages/zzstudio-think-addons)[topthink/think-annotation

Annotation For ThinkPHP

4831.5k8](/packages/topthink-think-annotation)[liliuwei/thinkphp-jump

适用于thinkphp6.0的跳转扩展

2874.4k1](/packages/liliuwei-thinkphp-jump)

PHPackages © 2026

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