Download the PHP package mehr-it/lara-token-bucket without Composer
On this page you can find all versions of the php package mehr-it/lara-token-bucket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mehr-it/lara-token-bucket
More information about mehr-it/lara-token-bucket
Files in mehr-it/lara-token-bucket
Package lara-token-bucket
Short Description Implements the token bucket algorithm using laravel cache
License MIT
Informations about the package lara-token-bucket
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
composer require mehr-it/lara-token-bucket
This package uses Laravel's package auto-discovery, so the service provider will be loaded automatically.
Usage
Use the TokenBucket
facade to access a token bucket:
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
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.
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:
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:
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: