yiisoft/mutex-file
Composer 安装命令:
composer require yiisoft/mutex-file
包简介
Yii Mutex Library - File Driver
README 文档
README
Yii Mutex Library - File Driver
This library provides a File mutex implementation for yiisoft/mutex.
Requirements
- PHP 7.4 or higher.
Installation
The package could be installed with Composer:
composer require yiisoft/mutex-file
General usage
The package provides two classes implementing MutexInterface and MutexFactoryInterface
from the yiisoft/mutex package:
$mutex = new \Yiisoft\Mutex\File\FileMutex( 'mutex-name', '/path/to/directory/for/storing/mutex/files', 0775, // Optional. The permission to be set for newly created mutex directory. Default is `0775`. 0777, // Optional. The permission to be set for newly created mutex files. Default is `null`. ); $mutexFactory = new \Yiisoft\Mutex\File\FileMutexFactory( '/path/to/directory/for/storing/mutex/files', 0775, // Optional. The permission to be set for newly created mutex directory. Default is `0775`. 0777, // Optional. The permission to be set for newly created mutex files. Default is `null`. );
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 = new \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 FileMutex 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 - File 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-file 适用场景与选型建议
yiisoft/mutex-file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.65k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2019 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii」 「mutex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/mutex-file 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/mutex-file 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/mutex-file 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 4.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-04-29