yiisoft/mutex-pdo-pgsql 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yiisoft/mutex-pdo-pgsql

最新稳定版本:1.1.0

Composer 安装命令:

composer require yiisoft/mutex-pdo-pgsql

包简介

Yii Mutex Library - Postgres PDO Driver

关键字:

README 文档

README

Yii

Yii Mutex Library - PostgreSQL PDO Driver


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

This library provides a PostgreSQL mutex implementation for yiisoft/mutex.

Requirements

  • PHP 7.4 or higher.
  • PDO PHP extension.

Installation

The package could be installed with Composer:

composer require yiisoft/mutex-pdo-pgsql

General usage

The package provides two classes implementing MutexInterface and MutexFactoryInterface from the yiisoft/mutex package:

/**
 * @var \PDO $connection Configured for PostgreSQL.
 */

$mutex = new \Yiisoft\Mutex\Pgsql\PgsqlMutex('mutex-name', $connection);

$mutexFactory = new \Yiisoft\Mutex\Pgsql\PgsqlMutexFactory($connection);

There are multiple ways you can use the package. You can execute a callback in a synchronized mode i.e. only a single instance of the callback is executed at the same time:

$synchronizer = new \Yiisoft\Mutex\Synchronizer($mutexFactory);

$newCount = $synchronizer->execute('critical', function () {
    return $counter->increase();
}, 10);

Another way is to manually open and close mutex:

$simpleMutex = \Yiisoft\Mutex\SimpleMutex($mutexFactory);

if (!$simpleMutex->acquire('critical', 10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$simpleMutex->release('critical');

It could be done on lower level:

$mutex = $mutexFactory->createAndAcquire('critical', 10);
$newCount = $counter->increase();
$mutex->release();

And if you want even more control, you can acquire mutex manually:

$mutex = $mutexFactory->create('critical');

if (!$mutex->acquire(10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$mutex->release();

The PgsqlMutex supports the "wait for a lock for a certain time" functionality. Using the withRetryDelay() method, you can override the number of milliseconds between each try until specified timeout times out:

$mutex = $mutex->withRetryDelay(100);

By default, it is 50 milliseconds - it means that we may try to acquire lock up to 20 times per second.

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Mutex Library - PostgreSQL PDO Driver is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 10
  • Watchers: 14
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2021-08-04