0 follower

Final Class Yiisoft\Mutex\Mysql\MysqlMutex

InheritanceYiisoft\Mutex\Mysql\MysqlMutex » Yiisoft\Mutex\Mutex

MysqlMutex implements mutex "lock" mechanism via MySQL locks.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Mutex\Mysql\MysqlMutex

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $name, PDO $connection )
$name string

Mutex name.

$connection PDO

PDO connection instance to use.

                public function __construct(string $name, PDO $connection)
{
    $this->lockName = sha1($name);
    $this->connection = $connection;
    /** @var string $driverName */
    $driverName = $connection->getAttribute(PDO::ATTR_DRIVER_NAME);
    if ($driverName !== 'mysql') {
        throw new InvalidArgumentException("MySQL connection instance should be passed. Got \"$driverName\".");
    }
    parent::__construct(self::class, $name);
}

            
acquireLock() protected method
protected acquireLock ( integer $timeout 0 )
$timeout integer

                protected function acquireLock(int $timeout = 0): bool
{
    $statement = $this->connection->prepare('SELECT GET_LOCK(:name, :timeout)');
    $statement->bindValue(':name', $this->lockName);
    $statement->bindValue(':timeout', $timeout);
    $statement->execute();
    return (bool) $statement->fetchColumn();
}

            
releaseLock() protected method
protected releaseLock ( )

                protected function releaseLock(): bool
{
    $statement = $this->connection->prepare('SELECT RELEASE_LOCK(:name)');
    $statement->bindValue(':name', $this->lockName);
    $statement->execute();
    return (bool) $statement->fetchColumn();
}