PHPackages                             token\_bucket\_middleware/token\_bucket - 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. [Caching](/categories/caching)
4. /
5. token\_bucket\_middleware/token\_bucket

ActiveLibrary[Caching](/categories/caching)

token\_bucket\_middleware/token\_bucket
=======================================

laravel+redis token bucket

011PHP

Since Jun 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/yokelili/mjn)[ Packagist](https://packagist.org/packages/token_bucket_middleware/token_bucket)[ RSS](/packages/token-bucket-middleware-token-bucket/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

laravel+redis token bucket
==========================

[](#laravelredis-token-bucket)

团队内部使用的令牌桶限流包

安装
--

[](#安装)

你可以通过`composer`进行安装

```
composer require token_bucket_middleware/token_bucket
```

使用
--

[](#使用)

1. 生成中间件

```
 php artisan make:middleware TokenBucketMiddleware
```

2. 修改`app/Http/Kernel.php`增加中间件

```
protected $routeMiddleware = [
    ...
    'tokenBucket' => \App\Http\Middleware\TokenBucketMiddleware::class,
]
```

3. 中间件编写

```
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Blog\TokenBucket\TokenBucket;

class TokenBucketMiddleware
{
    /**
     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        Log::info('***********************************');
        // 获取路由
        $url = $request->getRequestUri();
        $url = 'bucket:'.substr($url, 0);
        $tokenBucket = new TokenBucket;
        $result = $tokenBucket->grant($url);
        if (! $result) {
            Log::info('请求Discard'.date('s'));
            return response()->json(['status' => false, 'message' => 'network busy, please try again', 'code' => 429, 'data' => []]);
        }
        Log::info('请求成功'.date('s'));
        return $next($request);
    }
}
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/33946778?v=4)[yokelili](/maintainers/yokelili)[@yokelili](https://github.com/yokelili)

---

Top Contributors

[![yokelili](https://avatars.githubusercontent.com/u/33946778?v=4)](https://github.com/yokelili "yokelili (15 commits)")

### Embed Badge

![Health badge](/badges/token-bucket-middleware-token-bucket/health.svg)

```
[![Health](https://phpackages.com/badges/token-bucket-middleware-token-bucket/health.svg)](https://phpackages.com/packages/token-bucket-middleware-token-bucket)
```

###  Alternatives

[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21114.2k](/packages/iazaran-smart-cache)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

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