W3cubDocs

/Yii 2.0

Class yii\redis\Mutex

Inheritance yii\redis\Mutex » yii\mutex\Mutex » yii\base\Component » yii\base\Object
Implements yii\base\Configurable
Available since version 2.0.6
Source Code https://github.com/yiisoft/yii2-redis/blob/master/Mutex.php

Implements mutex based on Redis single instance lock algorithm.

See also:

  • http://redis.io/topics/distlock Application configuration example: [ 'components' => [ 'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, ], 'mutex' => [ 'class' => 'yii\redis\Mutex', 'redis' => 'redis', ], ], ]
  • yii\mutex\Mutex

Public Properties

Property Type Description Defined 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
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$keyPrefix string A string prefixed to every cache key so that it is unique. yii\redis\Mutex
$lockExpire integer Redis key expire, ms yii\redis\Mutex
$lockWaitSleep integer Sleep ms until next lock try during timeout waiting 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

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\Object
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
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\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
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

Method Description Defined By
acquireLock() This method should be extended by a concrete Mutex implementations. Acquires lock by name. yii\redis\Mutex
addLock() Adds lock to the local map yii\redis\Mutex
deleteLock() Deletes lock from the local map yii\redis\Mutex
getLock() Gets lock from the local map yii\redis\Mutex
getLockKey() Generates redis key for a lock name. yii\redis\Mutex
releaseLock() This method should be extended by a concrete Mutex implementations. Releases lock by given name. yii\redis\Mutex

Property Details

$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

$lockExpire public property

Redis key expire, ms

public integer $lockExpire = 3600000

$lockWaitSleep public property

Sleep ms until next lock try during timeout waiting

public integer $lockWaitSleep = 200

$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 Session object is created, if you want to change this property, you should only assign it with a Redis yii\redis\Connection object.

public yii\redis\Connection|string|array $redis = 'redis'

Method Details

acquireLock() protected method

This method should be extended by a concrete Mutex implementations. Acquires lock by name.

protected boolean acquireLock ( $name, $timeout = 0 )
$name string

Of the lock to be acquired.

$timeout integer

Time to wait for the lock to be released.

return boolean

Acquiring result.

addLock() protected method

Adds lock to the local map

protected array|null addLock ( $name )
$name string
return array|null

Lock's [keyName, keyValue] pair for redis or null if it's already exists in $lockMap

deleteLock() protected method

Deletes lock from the local map

protected boolean deleteLock ( $name )
$name string

getLock() protected method

Gets lock from the local map

protected array|null getLock ( $name )
$name string
return array|null

Lock's [keyName, keyValue] pair for redis or null if there is no such lock

getLockKey() protected method

Generates redis key for a lock name.

protected string getLockKey ( $lockName )
$lockName string

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

This method should be extended by a concrete Mutex implementations. Releases lock by given name.

protected boolean releaseLock ( $name )
$name string

Of the lock to be released.

return boolean

Release result.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-redis-mutex.html