Final Class Yiisoft\Mutex\Mysql\MysqlMutex
| Inheritance | Yiisoft\Mutex\Mysql\MysqlMutex » Yiisoft\Mutex\Mutex |
|---|
MysqlMutex implements mutex "lock" mechanism via MySQL locks.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Mutex\Mysql\MysqlMutex |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| acquireLock() | Yiisoft\Mutex\Mysql\MysqlMutex | |
| releaseLock() | Yiisoft\Mutex\Mysql\MysqlMutex |
Method Details
| 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);
}
See also:
| 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();
}
See also:
| 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();
}
Signup or Login in order to comment.