mata-sh/rate-limiter
Composer 安装命令:
composer require mata-sh/rate-limiter
包简介
Robust rate limiting library for PHP with support for (Redis | APCu | filesystem).
README 文档
README
Robust rate limiting library for PHP supporting filesystem storage backend for constrained environments.
Part of 🐢 mata-sh.
Features
- Supports Redis, APCu, Filesystem, and Session storage backends
- Zero configuration - works out of the box
- Flexible identifiers - IP-based, user-based, or custom
- PSR-3 logging support
- Sliding window rate limiting algorithm
- Proxy-aware IP detection (Cloudflare, X-Forwarded-For, etc.)
Installation
composer require mata-sh/rate-limiter
Requires PHP 8.1+. Redis or APCu recommended for production.
Usage
use MataSh\RateLimiter\RateLimiter; use MataSh\RateLimiter\RateLimitIdentifier; // Initialize with explicit storage backend $limiter = new RateLimiter('apcu'); // Simple identifier if ($limiter->allow('api_user_123', 10, 60)) { // Process request (10 requests per 60 seconds) } // IP-based limiting $identifier = RateLimitIdentifier::fromIp(); if (!$limiter->allow($identifier, 100, 3600)) { http_response_code(429); exit('Rate limit exceeded'); } // IP + username combined $identifier = RateLimitIdentifier::fromIpUser($username); $limiter->allow($identifier, 20, 60); // Check without consuming $status = $limiter->check($identifier, 100, 3600); header('X-RateLimit-Remaining: ' . $status['remaining']);
Identifiers
RateLimitIdentifier::fromIp(); // Auto-detect IP RateLimitIdentifier::fromIpUser($username); // IP + username RateLimitIdentifier::getClientIp(); // Get IP directly // Or create custom identifiers 'user_' . $userId 'api_key_' . hash('sha256', $apiKey)
Configuration
// Disable rate limiting (allows all requests) $limiter = new RateLimiter('apcu'); $limiter->setRateLimitingEnabled(false); // Set default limits $limiter->setRateLimitingConfig(100, 3600);
Storage Backends
Explicitly configure the storage backend on initialization:
APCu (Recommended)
$limiter = new RateLimiter('apcu');
Fast in-memory cache. Install: pecl install apcu
Redis
use MataSh\RateLimiter\RateLimiter; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('password'); $limiter = new RateLimiter('redis', null, $redis);
Best for distributed systems.
Filesystem
$limiter = new RateLimiter('file', '/var/app/cache/rate_limit');
JSON files with auto-cleanup. Default path: sys_get_temp_dir() . '/cache/rate_limit'
Session
$limiter = new RateLimiter('session');
Per-user limits only (not suitable for global IP-based limiting).
License
MIT
Related
Used by mata-dashboard and mata-node.
mata-sh/rate-limiter 适用场景与选型建议
mata-sh/rate-limiter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「php」 「file」 「rate limit」 「shared hosting」 「apcu」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mata-sh/rate-limiter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mata-sh/rate-limiter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mata-sh/rate-limiter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Laravel Media Popup to upload/view the files
PMVC Plugin for File information
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05