Stars: 754
Forks: 230
Pull Requests: 12
Issues: 8
Watchers: 25
Last Updated: 2020-08-11 17:36:09
Redis distributed locks in PHP
License: MIT License
Languages: PHP
Based on Redlock-rb by Salvatore Sanfilippo
This library implements the Redis-based distributed lock manager algorithm described in this blog post.
To create a lock manager:
$servers = [
['127.0.0.1', 6379, 0.01],
['127.0.0.1', 6389, 0.01],
['127.0.0.1', 6399, 0.01],
];
$redLock = new RedLock($servers);
To acquire a lock:
$lock = $redLock->lock('my_resource_name', 1000);
Where the resource name is an unique identifier of what you are trying to lock and 1000 is the number of milliseconds for the validity time.
The returned value is false
if the lock was not acquired (you may try again),
otherwise an array representing the lock is returned, having three keys:
Array
(
[validity] => 9897.3020019531
[resource] => my_resource_name
[token] => 53771bfa1e775
)
To release a lock:
$redLock->unlock($lock)
It is possible to setup the number of retries (by default 3) and the retry delay (by default 200 milliseconds) used to acquire the lock.
The retry delay is actually chosen at random between $retryDelay / 2
milliseconds and
the specified $retryDelay
value.
Disclaimer: As stated in the original antirez's version, this code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments.