https://github.com/arvenil/mutex
Mutex implementation for PHP
https://github.com/arvenil/mutex
flock lock memcache memcached mutex mysql php redis
Last synced: about 1 month ago
JSON representation
Mutex implementation for PHP
- Host: GitHub
- URL: https://github.com/arvenil/mutex
- Owner: arvenil
- License: mit
- Created: 2012-08-16T21:37:40.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2022-08-09T22:09:00.000Z (almost 3 years ago)
- Last Synced: 2025-04-02T03:17:45.631Z (about 2 months ago)
- Topics: flock, lock, memcache, memcached, mutex, mysql, php, redis
- Language: PHP
- Homepage:
- Size: 265 KB
- Stars: 189
- Watchers: 10
- Forks: 31
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](http://opensource.org/licenses/MIT)
[](https://packagist.org/packages/arvenil/ninja-mutex)
[](https://github.com/arvenil/ninja-mutex/releases/latest)[](https://github.com/arvenil/ninja-mutex/actions?query=workflow%3APHP)
[](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
[](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
[](https://codeclimate.com/github/arvenil/ninja-mutex)
[](https://insight.symfony.com/projects/15c5c748-f8d8-4b56-b536-a29a151aac6c)
[](https://packagist.org/packages/arvenil/ninja-mutex)## About
ninja-mutex is a simple to use mutex implementation for php. It supports different adapters (flock, memcache, mysql, redis, ...) so you can set it up as you wish. All adapters (if set up properly) can be used in multi server environment - in other words lock is shared between web servers.
## Usage
### Mutex
First you need to choose an adapter and setup it properly. For example if you choose flock implementation first you need to set up NFS filesystem and mount it on web servers. In this example we will choose memcache adapter:
```php
connect('127.0.0.1', 11211);
$lock = new MemcacheLock($memcache);
$mutex = new Mutex('very-critical-stuff', $lock);
if ($mutex->acquireLock(1000)) {
// Do some very critical stuff// and release lock after you finish
$mutex->releaseLock();
} else {
throw new Exception('Unable to gain lock!');
}
```### Mutex Fabric
If you want to use multiple mutexes in your project then MutexFabric is the right solution. Set up lock implementor once, and you can use as many mutexes as you want!
```php
connect('127.0.0.1', 11211);
$lock = new MemcacheLock($memcache);
$mutexFabric = new MutexFabric('memcache', $lock);
if ($mutexFabric->get('very-critical-stuff')->acquireLock(1000)) {
// Do some very critical stuff// and release lock after you finish
$mutexFabric->get('very-critical-stuff')->releaseLock();
} else {
throw new Exception('Unable to gain lock for very critical stuff!');
}if ($mutexFabric->get('also-very-critical-stuff')->acquireLock(0)) {
// Do some also very critical stuff// and release lock after you finish
$mutexFabric->get('also-very-critical-stuff')->releaseLock();
} else {
throw new Exception('Unable to gain lock for also very critical stuff!');
}
```## Installation
### Composer
Download composer:
wget -nc http://getcomposer.org/composer.phar
Add dependency to your project:
php composer.phar require arvenil/ninja-mutex:*
## Running tests
Tests require vfsStream to work. To install it, simply run in project dir:
wget -nc http://getcomposer.org/composer.phar && php composer.phar install --dev
To run tests type in a console:
vendor/bin/phpunit
## Something doesn't work
Feel free to fork project, fix bugs and finally request for pull