PHP Class malkusch\lock\mutex\RedisMutex

Note: If you're going to use this mutex in a forked process, you have to call {@link seedRandom()} in each instance.
Author: Markus Malkusch ([email protected])
Inheritance: extends SpinlockMutex, implements Psr\Log\LoggerAwareInterface
Show file Open project: malkusch/lock Class Usage Examples

Public Methods

Method Description
__construct ( array $redisAPIs, string $name, integer $timeout = 3 ) Sets the Redis APIs.
seedRandom ( integer | null $seed = null ) Seeds the random number generator.
setLogger ( Psr\Log\LoggerInterface $logger ) Sets a logger instance on the object

Protected Methods

Method Description
acquire ( $key, $expire )
add ( mixed $redisAPI, string $key, string $value, integer $expire ) : boolean Sets the key only if such key doesn't exist at the server yet.
evalScript ( mixed $redisAPI, string $script, integer $numkeys, array $arguments ) : mixed
getRedisIdentifier ( mixed $redisAPI ) : string Returns a string representation of the Redis API.
release ( $key )

Private Methods

Method Description
isMajority ( integer $count ) : boolean Returns if a count is the majority of all servers.

Method Details

__construct() public method

Sets the Redis APIs.
public __construct ( array $redisAPIs, string $name, integer $timeout = 3 )
$redisAPIs array The Redis APIs.
$name string The lock name.
$timeout integer The time in seconds a lock expires, default is 3.

acquire() protected method

protected acquire ( $key, $expire )

add() abstract protected method

Sets the key only if such key doesn't exist at the server yet.
abstract protected add ( mixed $redisAPI, string $key, string $value, integer $expire ) : boolean
$redisAPI mixed The connected Redis API.
$key string The key.
$value string The value.
$expire integer The TTL seconds.
return boolean True, if the key was set.

evalScript() abstract protected method

abstract protected evalScript ( mixed $redisAPI, string $script, integer $numkeys, array $arguments ) : mixed
$redisAPI mixed The connected Redis API.
$script string The Lua script.
$numkeys integer The number of arguments that represent Redis key names.
$arguments array Keys and values.
return mixed The script result, or false if executing failed.

getRedisIdentifier() abstract protected method

Returns a string representation of the Redis API.
abstract protected getRedisIdentifier ( mixed $redisAPI ) : string
$redisAPI mixed The connected Redis API.
return string The identifier.

release() protected method

protected release ( $key )

seedRandom() public method

Normally you don't need to seed, as this happens automatically. But if you experience a {@link LockReleaseException} this might come from identically created random tokens. In this case you could seed from /dev/urandom.
public seedRandom ( integer | null $seed = null )
$seed integer | null The optional seed.

setLogger() public method

RedLock is a fault tolerant lock algorithm. I.e. it does tolerate failing redis connections without breaking. If you want to get notified about such events you'll have to provide a logger. Those events will be logged as warnings.
public setLogger ( Psr\Log\LoggerInterface $logger )
$logger Psr\Log\LoggerInterface The logger.