PHPackages                             mehr-it/lara-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. mehr-it/lara-token-bucket

ActiveLibrary[Caching](/categories/caching)

mehr-it/lara-token-bucket
=========================

Implements the token bucket algorithm using laravel cache

1.1.1(4y ago)0731MITPHPPHP &gt;=7.3.0

Since Feb 10Pushed 4y agoCompare

[ Source](https://github.com/mehr-it/lara-token-bucket)[ Packagist](https://packagist.org/packages/mehr-it/lara-token-bucket)[ RSS](/packages/mehr-it-lara-token-bucket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (5)Used By (1)

Token bucket algorithm for laravel
==================================

[](#token-bucket-algorithm-for-laravel)

Implements the "Token bucket" algorithm using laravel's cache repositories.

**Note**: This implementation uses a clock resolution of one second. This means, new tokens are added every second at most.

Install
-------

[](#install)

```
composer require mehr-it/lara-token-bucket

```

This package uses Laravel's package auto-discovery, so the service provider will be loaded automatically.

Usage
-----

[](#usage)

Use the `TokenBucket` facade to access a token bucket:

```
// Create a token bucket with rate of 5 tokens per second
// and burst size of 20 tokens.
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20);

// try to take 2 tokens from the bucket
$success = $bucket->tryTake(2);
```

All token bucket instances with the same name use the same token store, as long as the use the same underlying cache.

### Estimate time until tokens are available

[](#estimate-time-until-tokens-are-available)

Sometimes, it might be helpful to estimate the duration until new tokens become available. The `tryTake()` method returns the estimated time to the second parameter if given. If you just want to check the estimated availability without taking tokens out, the `estimateAvailability()` method is what you are looking for.

```
// $secUntilAvailableNext is filled with the duration until
// another 2 tokens are available after an eventually
// successful taking
$bucket->tryTake(2, $secUntilAvailableNext);

// returns the duration until 2 tokens are available
$secUntilAvailableNext = $bucket->estimateAvailability(3);
```

### Putting tokens back

[](#putting-tokens-back)

Sometimes, you might have taken a token which you didn't need. You can give tokens back to buckets without affecting the time-based filling:

```
// give back 2 tokens
$bucket->putTokens(2);
```

### Predefined buckets

[](#predefined-buckets)

If you want to predefine token buckets and use them later by resolving the bucket name, the `registerBucket()` method is what you need:

```
// register
\TokenBucket::registerBucket('myBucket', 5.0, 20);

// resolve
$bucket = \TokenBucket::resolveBucket('myBucket');
```

Edge cases
----------

[](#edge-cases)

By default, buckets are empty when no data exists. This happens the first time a bucket is requested or after a cache flush.

You may define the initial number of tokens a bucket holds, when creating a bucket instance:

```
// create a token bucket with initial token number of 3
$bucket = \TokenBucket::bucket('myBucket', 5.0, 20, 3);
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

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

Total

4

Last Release

1547d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/44973729?v=4)[mehr.IT GmbH](/maintainers/mehr-it)[@mehr-it](https://github.com/mehr-it)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mehr-it-lara-token-bucket/health.svg)

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

###  Alternatives

[imanghafoori/laravel-widgetize

A minimal yet powerful package to give a better structure and caching opportunity for your Laravel apps.

909137.9k12](/packages/imanghafoori-laravel-widgetize)[swayok/alternative-laravel-cache

Replacements for Laravel's redis and file cache stores that properly implement tagging idea. Powered by cache pool implementations provided by http://www.php-cache.com/

202541.1k6](/packages/swayok-alternative-laravel-cache)[alexmg86/laravel-sub-query

Laravel subquery

7538.4k](/packages/alexmg86-laravel-sub-query)[laravel-enso/rememberable

Model caching dependency for Laravel Enso

2863.2k25](/packages/laravel-enso-rememberable)[byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.

1413.9k](/packages/byerikas-cache-tags)

PHPackages © 2026

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