Download the PHP package ballyhoo-technic/laravel-redlock without Composer
On this page you can find all versions of the php package ballyhoo-technic/laravel-redlock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ballyhoo-technic/laravel-redlock
More information about ballyhoo-technic/laravel-redlock
Files in ballyhoo-technic/laravel-redlock
Package laravel-redlock
Short Description Redis distributed locks for laravel
License MIT
Informations about the package laravel-redlock
Laravel RedLock
Provides a generic locking mechanism using Redis. Implements the locking standard proposed by Redis.
Acknowledgements
This library was originally built by LibiChai based on the Redlock algorithm developed by antirez. The library was reworked by the team at That's Us, Inc.
Regarding the new laravel 6.x LTS releases, Ballyhoo Werbeagentur GmbH did this fork, to provide a compatible version. All credits still goes to LibiChai and That's Us, Inc. Thank you for the greate work.
Installation
composer require ballyhoo-technic/laravel-redlock
- Add
ThatsUs\RedLock\RedLockServiceProvider::class,
to theproviders
array in config/app.php - Enjoy!
It's Simple!
Set a lock on any scalar. If the lock()
method returns false, you did not acquire the lock.
Store results of the lock()
method. Use this value to release the lock with unlock()
.
Example
This example sets a lock on the key "1" with a 3 second expiration time.
If it acquired the lock, it does some work and releases the lock.
Refresh
Use refreshLock()
to reacquire and extend the time of your lock.
Even Easier with Closures
Use runLocked()
for nicer syntax. The method returns the results of the closure, or else false if the lock could not be acquired.
Refresh with Closures
You can easily refresh your tokens when using closures. The first parameter to your closure is $refresh
. Simply call it when you want to refresh. If the lock cannot be refreshed, $refresh()
will break out of the closure.
Lock Queue Jobs Easily
If you're running jobs on a Laravel queue, you may want to avoid queuing up the same job more than once at a time.
The ThatsUs\RedLock\Traits\QueueWithoutOverlap
trait provides this functionality with very few changes to your job. Usually only two changes are necessary.
use ThatsUs\RedLock\Traits\QueueWithoutOverlap
as a trait- Change the
handle()
method tohandleSync()
Sometimes it's also necessary to specify a getLockKey()
method. This method must return the string that needs to be locked.
This is typically unnecessary because the lock key can be generated automatically. But if the job's data is not easy to stringify, you must define the getLockKey()
method.
This trait also provides a refresh method called refreshLock()
. If refreshLock()
is unable to refresh the lock, an exception is thrown and the job fails.
Finally, you can change the lock time-to-live from the default 300 seconds to another
value using the $lock_time
property.
Contribution
If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.