PHPackages                             mata-sh/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. [Caching](/categories/caching)
4. /
5. mata-sh/rate-limiter

ActiveLibrary[Caching](/categories/caching)

mata-sh/rate-limiter
====================

Robust rate limiting library for PHP with support for (Redis | APCu | filesystem).

v0.1.0(4mo ago)028MITPHPPHP ^8.0CI passing

Since Jan 5Pushed 4mo agoCompare

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

READMEChangelogDependencies (3)Versions (2)Used By (0)

Rate Limiter
============

[](#rate-limiter)

Robust rate limiting library for PHP supporting filesystem storage backend for constrained environments.

Part of 🐢 [mata-sh](https://github.com/mata-sh).

Features
--------

[](#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
------------

[](#installation)

```
composer require mata-sh/rate-limiter
```

Requires PHP 8.1+. Redis or APCu recommended for production.

Usage
-----

[](#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

[](#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

[](#configuration)

```
// Disable rate limiting (allows all requests)
$limiter = new RateLimiter('apcu');
$limiter->setRateLimitingEnabled(false);

// Set default limits
$limiter->setRateLimitingConfig(100, 3600);
```

Storage Backends
----------------

[](#storage-backends)

Explicitly configure the storage backend on initialization:

### APCu (Recommended)

[](#apcu-recommended)

```
$limiter = new RateLimiter('apcu');
```

Fast in-memory cache. Install: `pecl install apcu`

### Redis

[](#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

[](#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

[](#session)

```
$limiter = new RateLimiter('session');
```

Per-user limits only (not suitable for global IP-based limiting).

License
-------

[](#license)

MIT

Related
-------

[](#related)

Used by [mata-dashboard](https://github.com/mata-sh/mata-dashboard) and [mata-node](https://github.com/mata-sh/mata-node).

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance77

Regular maintenance activity

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity29

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

127d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

phpredisfilerate-limiterapcurate limitminimal-dependenciesshared hosting

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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