https://github.com/swordev/mutex-php
https://github.com/swordev/mutex-php
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/swordev/mutex-php
- Owner: swordev
- License: mit
- Created: 2020-07-11T10:33:05.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T12:11:32.000Z (almost 6 years ago)
- Last Synced: 2025-08-18T05:56:48.323Z (10 months ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Mutex
> Read-write concurrency control
## Installation
```sh
composer require @swordev/mutex
```
## Usage
### File read/write lock
```php
use Swordev\Mutex\FileMutex;
$mutex1 = new FileMutex('key');
$mutex2 = new FileMutex('key');
$mutex1->readLock(); // true
$mutex2->writeLock(); // false
$mutex1->unlock(); // true
$mutex2->writeLock(); // true
```
### Timeout
```php
use Swordev\Mutex\FileMutex;
$mutex = new FileMutex('key');
$mutex->writeLock(5000);
```
### Contextual lock
```php
use Swordev\Mutex\FileMutex;
class Foo {
function method() {
$mutex = new FileMutex(__CLASS__ . '|' . __FUNCTION__);
$mutex->writeLock();
// ...
}
}
```
### Mutex factory
```php
use Swordev\Mutex\MutexFactory;
$mutex = new MutexFactory::create(FileMutex::class, 'key');
```
## Development
### Test
```sh
composer run test
```
### Analyse
```sh
composer run analyse
```
## Author
Juanra GM - https://github.com/juanrgm
Distributed under the MIT license.
[https://github.com/swordev/mutex-php](https://github.com/swordev/mutex-php)