dev-master
9999999-devRedis distributed locks in PHP
The Requires
- php ~5.4
- predis/predis ^1.1
by Ronny Lopez
by Artyom Kohver
Wallogit.com
2017 © Pedro Peláez
Redis distributed locks in PHP
redlock-php - Redis distributed locks in PHP, (*1)
Based on Redlock-rb by Salvatore Sanfilippo, (*2)
This library implements the Redis-based distributed lock manager algorithm described in this blog post., (*3)
To create a lock manager:, (*4)
$RedLock = new RedLock([ new Client(['host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0.01]), new Client(['host' => '127.0.0.1', 'port' => 6380, 'timeout' => 0.01]), new Client(['host' => '127.0.0.1', 'port' => 6381, 'timeout' => 0.01]), ]);
To acquire a lock:, (*5)
$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., (*6)
If the lock was not acquired LockTimeoutException will be thrown,
otherwise an instance of Lock is returned, having three methods:, (*7)
getValidity, an integer representing the number of milliseconds the lock will be valid.getResource, the name of the locked resource as specified by the user.getToken, a random token value which is used to safe reclaim the lock.To release a lock:, (*8)
$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., (*9)
The retry delay is actually chosen at random between $retryDelay / 2 milliseconds and
the specified $retryDelay value., (*10)
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., (*11)
Redis distributed locks in PHP