Class yii\redis\Mutex
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Available since extension's version | 2.0.6 |
| Source Code | https://github.com/yiisoft/yii2-redis/blob/master/src/Mutex.php |
Redis Mutex implements a mutex component using redis as the storage medium.
Redis Mutex requires redis version 2.6.12 or higher to work properly.
It needs to be configured with a redis yii\redis application component.
To use redis Mutex as the application component, configure the application as follows:
[
'components' => [
'mutex' => [
'class' => 'yii\redis\Mutex' ,
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
]
Or if you have configured the redis yii\
[
'components' => [
'mutex' => [
'class' => 'yii\redis\Mutex' ,
// 'redis' => 'redis' // id of the connection application component
],
],
]
See also:
- \
yii\ mutex\ Mutex - https://redis.io/topics/distlock
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $expire | integer | The number of seconds in which the lock will be auto released. | yii\ |
| $keyPrefix | string | A string prefixed to every cache key so that it is unique. | yii\ |
| $redis | yii\ |
The Redis yii\ |
yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| init() | Initializes the redis Mutex component. | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| acquireLock() | Acquires a lock by name. | yii\ |
| calculateKey() | Generates a unique key used for storing the mutex in Redis. | yii\ |
| releaseLock() | Releases acquired lock. This method will return false in case the lock was not found or Redis command failed. |
yii\ |
Property Details
The number of seconds in which the lock will be auto released.
A string prefixed to every cache key so that it is unique. If not set, it will use a prefix generated from Application::id. You may set this property to be an empty string if you don't want to use key prefix. It is recommended that you explicitly set this property to some static value if the cached data needs to be shared among multiple applications.
The Redis yii\
Method Details
Acquires a lock by name.
| protected boolean acquireLock ( string $name, integer $timeout = 0 ) | ||
| $name | string |
Of the lock to be acquired. Must be unique. |
| $timeout | integer |
Time (in seconds) to wait for lock to be released. Defaults to |
| return | boolean |
Lock acquiring result. |
|---|---|---|
protected function acquireLock($name, $timeout = 0)
{
$key = $this->calculateKey($name);
$value = Yii::$app->security->generateRandomString(20);
$result = $this->retryAcquire($timeout, function () use ($key, $value) {
return $this->redis->executeCommand('SET', [$key, $value, 'NX', 'PX', (int) ($this->expire * 1000)]);
});
if ($result) {
$this->_lockValues[$name] = $value;
}
return $result;
}
Generates a unique key used for storing the mutex in Redis.
| protected string calculateKey ( string $name ) | ||
| $name | string |
Mutex name. |
| return | string |
A safe cache key associated with the mutex name. |
|---|---|---|
protected function calculateKey($name)
{
return $this->keyPrefix . md5(json_encode([__CLASS__, $name]));
}
Initializes the redis Mutex component.
This method will initialize the $redis property to make sure it refers to a valid redis connection.
| public mixed init ( ) | ||
| throws | \ |
if $redis is invalid. |
|---|---|---|
public function init()
{
parent::init();
$this->redis = Instance::ensure($this->redis, Connection::className());
if ($this->keyPrefix === null) {
$this->keyPrefix = substr(md5(Yii::$app->id), 0, 5);
}
}
Releases acquired lock. This method will return false in case the lock was not found or Redis command failed.
| protected boolean releaseLock ( string $name ) | ||
| $name | string |
Of the lock to be released. This lock must already exist. |
| return | boolean |
Lock release result: |
|---|---|---|
protected function releaseLock($name)
{
static $releaseLuaScript = <<<LUA
edis.call("GET",KEYS[1])==ARGV[1] then
return redis.call("DEL",KEYS[1])
return 0
if (
!isset($this->_lockValues[$name])
|| !$this->redis->executeCommand('EVAL', [
$releaseLuaScript,
1,
$this->calculateKey($name),
$this->_lockValues[$name],
])
) {
return false;
}
unset($this->_lockValues[$name]);
return true;
}