yiisoft/mutex-redis
Composer 安装命令:
composer require yiisoft/mutex-redis
包简介
Yii Mutex Library - Redis Driver
README 文档
README
Yii Mutex Library - Redis Driver
This library provides a Redis mutex implementation for yiisoft/mutex.
Requirements
- PHP 7.4 or higher.
Installation
The package could be installed with Composer:
composer require yiisoft/mutex-redis
General usage
The package provides two classes implementing MutexInterface and MutexFactoryInterface
from the yiisoft/mutex package:
/** * @var \Predis\ClientInterface $client Predis client instance to use. */ $mutex = new \Yiisoft\Mutex\Redis\RedisMutex( 'mutex-name', $client, 60, // Optional. Number of seconds in which the lock will be auto released. Default is `30`. ); $mutexFactory = new \Yiisoft\Mutex\Redis\RedisMutexFactory( $client, 60, // Optional. Number of seconds in which the lock will be auto released. Default is `30`. );
For more information about the client instance and connection configuration, see the documentation of the predis/predis package.
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 RedisMutex 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 - Redis 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
Follow updates
yiisoft/mutex-redis 适用场景与选型建议
yiisoft/mutex-redis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95.63k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2021 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii」 「mutex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/mutex-redis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/mutex-redis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/mutex-redis 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CacheMutex for Yii2
Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking
A Symfony bundle for Mutex implementation for PHP
Run mutually exclusive migrations from more than one server at a time
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Symfony Lock Component
统计信息
- 总下载量: 95.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-08-18