PHPackages                             andrewdyer/predis-request-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. andrewdyer/predis-request-limiter

ActiveLibrary[Caching](/categories/caching)

andrewdyer/predis-request-limiter
=================================

A framework-agnostic PHP library for rate limiting requests using Predis

3.0.0(1mo ago)1554MITPHPPHP ^8.3CI passing

Since Jul 9Pushed 4w agoCompare

[ Source](https://github.com/andrewdyer/predis-request-limiter)[ Packagist](https://packagist.org/packages/andrewdyer/predis-request-limiter)[ Docs](https://github.com/andrewdyer/predis-request-limiter)[ RSS](/packages/andrewdyer-predis-request-limiter/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

Predis Request Limiter
======================

[](#predis-request-limiter)

A framework-agnostic PHP library for rate limiting requests using [Predis](https://github.com/predis/predis).

[![Latest Stable Version](https://camo.githubusercontent.com/025b2d60e14305fbcc0ceb28b93e94e682fcca61b8a930775032524a79a62536/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7072656469732d726571756573742d6c696d697465722f763f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/predis-request-limiter)[![Total Downloads](https://camo.githubusercontent.com/344f3f6c63dccaa43a602d7df08b1878b2d0c1cc8f6a471e7ab51d800b648429/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7072656469732d726571756573742d6c696d697465722f646f776e6c6f6164733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/predis-request-limiter)[![License](https://camo.githubusercontent.com/6fab44be15e10540f2f74381f3c9bfda29f955893755c293ec9d52e7d89b3fbc/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7072656469732d726571756573742d6c696d697465722f6c6963656e73653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/predis-request-limiter)[![PHP Version Require](https://camo.githubusercontent.com/993d6166cd599ed7ea6026260fe117a005440bd8449040193ba65cb8a2eb6f81/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7072656469732d726571756573742d6c696d697465722f726571756972652f7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/predis-request-limiter)

Introduction
------------

[](#introduction)

This library provides a request rate limiter for PHP applications, backed by Redis via Predis. The rate limit window, request threshold, storage key, and limit exceeded handler are all configurable, and any Predis-compatible client can be used as the backing store. The package is built on top of [andrewdyer/php-package-template](https://github.com/andrewdyer/php-package-template).

Prerequisites
-------------

[](#prerequisites)

- **[PHP](https://www.php.net/)**: Version 8.3 or higher is required.
- **[Composer](https://getcomposer.org/)**: Dependency management tool for PHP.
- **[Redis](https://redis.io/)**: A running Redis instance is required.

Installation
------------

[](#installation)

```
composer require andrewdyer/predis-request-limiter
```

Getting Started
---------------

[](#getting-started)

### 1. Create a Predis client

[](#1-create-a-predis-client)

```
use Predis\Client;

$client = new Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]);
```

### 2. Create a limiter

[](#2-create-a-limiter)

Instantiate `Limiter` with a Predis client and a unique identifier. The identifier is interpolated into the storage key to namespace requests per user, IP, or endpoint:

```
use AndrewDyer\PredisRequestLimiter\Limiter;

$limiter = new Limiter($client, '127.0.0.1');
```

### 3. Configure the rate limit

[](#3-configure-the-rate-limit)

Set the maximum number of requests and the time window in seconds:

```
$limiter->setRateLimit(requests: 10, perSecond: 60);
```

Usage
-----

[](#usage)

### Checking and incrementing

[](#checking-and-incrementing)

Check whether the limit has been exceeded before incrementing the request count:

```
if ($limiter->hasExceededRateLimit()) {
    // Limit exceeded — respond with 429 or invoke the handler
} else {
    $limiter->incrementRequestCount();
}
```

### Setting a limit exceeded handler

[](#setting-a-limit-exceeded-handler)

Register a callable to invoke when the limit is exceeded:

```
$limiter->setLimitExceededHandler(function (): void {
    http_response_code(429);
    echo 'Too many requests.';
    exit;
});
```

Then invoke it when the limit is exceeded:

```
if ($limiter->hasExceededRateLimit()) {
    ($limiter->getLimitExceededHandler())();
} else {
    $limiter->incrementRequestCount();
}
```

### Customising the storage key

[](#customising-the-storage-key)

The default storage key template is `rate:%s:requests`, where `%s` is replaced by the identifier. Override it to namespace keys differently:

```
$limiter->setStorageKey('api:limit:%s');
```

With the identifier `127.0.0.1`, the resolved key becomes `api:limit:127.0.0.1`.

License
-------

[](#license)

Licensed under the [MIT licence](https://opensource.org/licenses/MIT) and is free for private or commercial projects.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance94

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity77

Established project with proven stability

 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 ~838 days

Total

4

Last Release

33d ago

Major Versions

v1.0.0 → 2.0.02023-01-26

2.0.1 → 3.0.02026-05-28

PHP version history (3 changes)v1.0.0PHP ^7.1.3

2.0.0PHP ^7.4 || ^8.0

3.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/666597ea6e46748a89fe8764d1a45b4d0da97daf1bb1e9770ea34ae41f706d08?d=identicon)[andrewdyer](/maintainers/andrewdyer)

---

Top Contributors

[![andrewdyer](https://avatars.githubusercontent.com/u/8114523?v=4)](https://github.com/andrewdyer "andrewdyer (18 commits)")

---

Tags

framework-agnosticphppredisrate-limitredisphpredispredisrate limitframework agnostic

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/andrewdyer-predis-request-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/andrewdyer-predis-request-limiter/health.svg)](https://phpackages.com/packages/andrewdyer-predis-request-limiter)
```

###  Alternatives

[mjphaynes/php-resque

Redis backed library for creating background jobs and processing them later.

225200.7k2](/packages/mjphaynes-php-resque)[predis/service-provider

Predis service provider for the Silex microframework

68548.0k1](/packages/predis-service-provider)[maykonn/codeigniter-predis

The CodeIgniter Redis package

1210.4k](/packages/maykonn-codeigniter-predis)[quick/cache

This is a cache system that uses Redis for rapid caching.

122.7k](/packages/quick-cache)

PHPackages © 2026

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