Class yii\redis\Mutex

Inheritanceyii\redis\Mutex » yii\mutex\Mutex » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0.6
Source Code https://github.com/yiisoft/yii2-redis/blob/master/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\Connection that is also configured as an application component. By default it will use the 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\redis\Connection as an application component, the following is sufficient:

[
    'components' => [
        'mutex' => [
            'class' => 'yii\redis\Mutex',
            // 'redis' => 'redis' // id of the connection application component
        ],
    ],
]

See also:

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$autoRelease boolean Whether all locks acquired in this process (i.e. local locks) must be released automatically before finishing script execution. yii\mutex\Mutex
$expire integer The number of seconds in which the lock will be auto released. yii\redis\Mutex
$keyPrefix string A string prefixed to every cache key so that it is unique. yii\redis\Mutex
$redis yii\redis\Connection|string|array The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. yii\redis\Mutex

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
acquire() Acquires a lock by name. yii\mutex\Mutex
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the redis Mutex component. yii\redis\Mutex
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
release() Releases acquired lock. This method will return false in case the lock was not found. yii\mutex\Mutex
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
acquireLock() Acquires a lock by name. yii\redis\Mutex
calculateKey() Generates a unique key used for storing the mutex in Redis. yii\redis\Mutex
releaseLock() Releases acquired lock. This method will return false in case the lock was not found or Redis command failed. yii\redis\Mutex

Property Details

$expire public property

The number of seconds in which the lock will be auto released.

public integer $expire 30
$keyPrefix public property

A string prefixed to every cache key so that it is unique. If not set, it will use a prefix generated from \yii\redis\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.

public string $keyPrefix null
$redis public property

The Redis yii\redis\Connection object or the application component ID of the Redis yii\redis\Connection. This can also be an array that is used to create a redis yii\redis\Connection instance in case you do not want do configure redis connection as an application component. After the Mutex object is created, if you want to change this property, you should only assign it with a Redis yii\redis\Connection object.

Method Details

acquireLock() protected method

Acquires a lock by name.

protected boolean acquireLock ( $name, $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 0 meaning that method will return false immediately in case lock was already acquired.

return boolean

Lock acquiring result.

calculateKey() protected method

Generates a unique key used for storing the mutex in Redis.

protected string calculateKey ( $name )
$name string

Mutex name.

return string

A safe cache key associated with the mutex name.

init() public method

Initializes the redis Mutex component.

This method will initialize the $redis property to make sure it refers to a valid redis connection.

public void init ( )
throws yii\base\InvalidConfigException

if $redis is invalid.

releaseLock() protected method

Releases acquired lock. This method will return false in case the lock was not found or Redis command failed.

protected boolean releaseLock ( $name )
$name string

Of the lock to be released. This lock must already exist.

return boolean

Lock release result: false in case named lock was not found or Redis command failed.