Final Class Yiisoft\Mutex\Pgsql\PgsqlMutex
| Inheritance | Yiisoft\Mutex\Pgsql\PgsqlMutex » Yiisoft\Mutex\Mutex |
|---|
PgsqlMutex implements mutex "lock" mechanism via PostgreSQL locks.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Mutex\Pgsql\PgsqlMutex |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| acquireLock() | Yiisoft\Mutex\Pgsql\PgsqlMutex | |
| releaseLock() | Yiisoft\Mutex\Pgsql\PgsqlMutex |
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)
{
// Converts a string into two 16-bit integer keys using the SHA1 hash function.
$this->lockKeys = array_values(unpack('n2', sha1($name, true)));
$this->connection = $connection;
/** @var string $driverName */
$driverName = $connection->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driverName !== 'pgsql') {
throw new InvalidArgumentException("PostgreSQL connection instance should be passed. Got \"$driverName\".");
}
parent::__construct(self::class, $name);
}
| protected acquireLock ( integer $timeout = 0 ) | ||
| $timeout | integer | |
protected function acquireLock(int $timeout = 0): bool
{
$statement = $this->connection->prepare('SELECT pg_try_advisory_lock(:key1, :key2)');
$statement->bindValue(':key1', $this->lockKeys[0]);
$statement->bindValue(':key2', $this->lockKeys[1]);
$statement->execute();
return (bool) $statement->fetchColumn();
}
| protected releaseLock ( ) |
protected function releaseLock(): bool
{
$statement = $this->connection->prepare('SELECT pg_advisory_unlock(:key1, :key2)');
$statement->bindValue(':key1', $this->lockKeys[0]);
$statement->bindValue(':key2', $this->lockKeys[1]);
$statement->execute();
return (bool) $statement->fetchColumn();
}
Signup or Login in order to comment.