https://github.com/yiisoft/mutex-file
File-based mutex implementation for yiisoft/mutex.
https://github.com/yiisoft/mutex-file
file hacktoberfest mutex yii3
Last synced: 8 months ago
JSON representation
File-based mutex implementation for yiisoft/mutex.
- Host: GitHub
- URL: https://github.com/yiisoft/mutex-file
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2019-04-14T09:50:54.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-04T11:13:31.000Z (about 2 years ago)
- Last Synced: 2024-04-13T22:58:00.723Z (about 2 years ago)
- Topics: file, hacktoberfest, mutex, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 89.8 KB
- Stars: 12
- Watchers: 17
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Mutex Library - File Driver
[](https://packagist.org/packages/yiisoft/mutex-file)
[](https://packagist.org/packages/yiisoft/mutex-file)
[](https://github.com/yiisoft/mutex-file/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/mutex-file)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/mutex-file/master)
[](https://github.com/yiisoft/mutex-file/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/mutex-file)
This library provides a File mutex implementation for [yiisoft/mutex](https://github.com/yiisoft/mutex).
## Requirements
- PHP 7.4 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/mutex-file
```
## General usage
The package provides two classes implementing `MutexInterface` and `MutexFactoryInterface`
from the [yiisoft/mutex](https://github.com/yiisoft/mutex) package:
```php
$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:
```php
$synchronizer = new \Yiisoft\Mutex\Synchronizer($mutexFactory);
$newCount = $synchronizer->execute('critical', function () {
return $counter->increase();
}, 10);
```
Another way is to manually open and close mutex:
```php
$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:
```php
$mutex = $mutexFactory->createAndAcquire('critical', 10);
$newCount = $counter->increase();
$mutex->release();
```
And if you want even more control, you can acquire mutex manually:
```php
$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:
```php
$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
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
## License
The Yii Mutex Library - File Driver is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.
Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)