yiisoft / mutex-pdo-oracle
Yii Mutex 库 - Oracle PDO 驱动
1.1.0
2021-10-19 07:17 UTC
Requires
- php: ^7.4|^8.0
- ext-pdo: *
- yiisoft/mutex: ^1.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- roave/infection-static-analysis-plugin: ^1.9
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.9
This package is auto-updated.
Last update: 2024-09-20 11:14:49 UTC
README
Yii Mutex 库 - Oracle PDO 驱动
此库为 yiisoft/mutex 提供了 Oracle mutex 实现。
要求
- PHP 7.4 或更高版本。
PDO
PHP 扩展。
安装
可以使用 Composer 安装此包。
composer require yiisoft/mutex-pdo-oracle
通用用法
该包提供了两个类,实现了来自 yiisoft/mutex 包的 MutexInterface
和 MutexFactoryInterface
。
use Yiisoft\Mutex\Oracle\OracleMutex; use Yiisoft\Mutex\Oracle\OracleMutexFactory; /** * @var \PDO $connection Configured for Oracle. */ $mutex = new OracleMutex( 'mutex-name', $connection, OracleMutex::MODE_X, // Optional. Lock mode to be used. Default is `OracleMutex::MODE_X`. false, // Optional. Whether to release lock on commit. Default is `false`. ); $mutexFactory = new OracleMutexFactory( $connection, OracleMutex::MODE_X, // Optional. Lock mode to be used. Default is `OracleMutex::MODE_X`. false, // Optional. Whether to release lock on commit. Default is `false`. );
有关“锁定模式”和“在提交时释放锁”选项的更多信息,请参阅 Oracle 文档。
您可以使用多种方式使用此包。您可以在同步模式下执行回调,即同一时间只执行一个回调实例
$synchronizer = new \Yiisoft\Mutex\Synchronizer($mutexFactory); $newCount = $synchronizer->execute('critical', function () { return $counter->increase(); }, 10);
另一种方法是手动打开和关闭互斥锁
$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');
这可以在较低级别完成
$mutex = $mutexFactory->createAndAcquire('critical', 10); $newCount = $counter->increase(); $mutex->release();
如果您想要更多的控制,您还可以手动获取互斥锁
$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();
OracleMutex
支持等待一定时间获取锁的功能。使用 withRetryDelay()
方法,您可以覆盖每次尝试之间的毫秒数,直到指定的超时时间超时
$mutex = $mutex->withRetryDelay(100);
默认情况下,它是 50 毫秒 - 这意味着我们每秒可能尝试获取锁多达 20 次。
文档
如果您需要帮助或有问题,Yii 论坛 是一个不错的选择。您还可以查看其他 Yii 社区资源。
许可证
Yii Mutex Library - Oracle PDO Driver 是免费软件。它根据 BSD 许可证的条款发布。有关更多信息,请参阅 LICENSE
。
由 Yii 软件 维护。