PHP Classes

Ninja Mutex: Lock resources to prevent simultaneous accesses

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (9 months ago) RSS 2.0 feedNot enough user ratingsTotal: 176 All time: 8,767 This week: 206Up
Version License PHP version Categories
ninja-mutex 0.6.11MIT/X Consortium ...5.3PHP 5, Language
Description 

Author

This package can lock resources to prevent simultaneous accesses from different processes.

It provides several classes that use mutually exclusive (mutexes) mechanisms to prevent that more than one process can change the resource at the same time.

Currently it provides mutex classes file locks (flock), memcache, MySQL and Redis.

Picture of Kamil Dziedzic
  Performance   Level  
Name: Kamil Dziedzic <contact>
Classes: 1 package by
Country: Poland Poland
Age: 39
All time rank: 421797 in Poland Poland
Week rank: 227 Up3 in Poland Poland Up

Details

[![License](https://img.shields.io/github/license/arvenil/ninja-mutex?color=informational)](http://opensource.org/licenses/MIT) [![PHP](https://img.shields.io/packagist/php-v/arvenil/ninja-mutex)](https://packagist.org/packages/arvenil/ninja-mutex) [![Version](https://img.shields.io/github/v/release/arvenil/ninja-mutex)](https://github.com/arvenil/ninja-mutex/releases/latest) [![Build](https://github.com/arvenil/ninja-mutex/workflows/PHP/badge.svg)](https://github.com/arvenil/ninja-mutex/actions?query=workflow%3APHP) [![Coverage](https://img.shields.io/scrutinizer/coverage/g/arvenil/ninja-mutex?color=success)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master) [![Quality](https://img.shields.io/scrutinizer/quality/g/arvenil/ninja-mutex?color=success&label=quality)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master) [![Maintainability](https://img.shields.io/codeclimate/maintainability-percentage/arvenil/ninja-mutex?color=success)](https://codeclimate.com/github/arvenil/ninja-mutex) [![Grade](https://img.shields.io/symfony/i/grade/15c5c748-f8d8-4b56-b536-a29a151aac6c?color=success)](https://insight.symfony.com/projects/15c5c748-f8d8-4b56-b536-a29a151aac6c) [![Total Downloads](https://img.shields.io/packagist/dt/arvenil/ninja-mutex.svg)](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 <?php require 'vendor/autoload.php'; use NinjaMutex\Lock\MemcacheLock; use NinjaMutex\Mutex; $memcache = new Memcache(); $memcache->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 <?php require 'vendor/autoload.php'; use NinjaMutex\Lock\MemcacheLock; use NinjaMutex\MutexFabric; $memcache = new Memcache(); $memcache->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

  Files folder image Files (45)  
File Role Description
Files folder image.github (1 file, 1 directory)
Files folder imagesrc (4 files, 1 directory)
Files folder imagetests (4 files, 2 directories)
Accessible without login Plain text file .env Data Auxiliary data
Accessible without login Plain text file composer.json Data Composer requires
Accessible without login Plain text file dependabot.yml Data Auxiliary data
Accessible without login Plain text file docker-compose.test.yml Data Auxiliary data
Accessible without login Plain text file Dockerfile Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License MIT
Accessible without login Plain text file Makefile Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Data Readme
Accessible without login Plain text file wait-for Data Auxiliary data

  Files folder image Files (45)  /  .github  
File Role Description
Files folder imageworkflows (1 file)
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files (45)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file PHP.yml Data Auxiliary data

  Files folder image Files (45)  /  src  
File Role Description
Files folder imageLock (13 files)
  Plain text file Mutex.php Class Class source
  Plain text file MutexException.php Class Class source
  Plain text file MutexFabric.php Class Class source
  Plain text file UnrecoverableMutexException.php Class Class source

  Files folder image Files (45)  /  src  /  Lock  
File Role Description
  Plain text file BasicLockInformationProvider.php Class Class source
  Plain text file DirectoryLock.php Class Class source
  Plain text file FlockLock.php Class Class source
  Plain text file LockAbstract.php Class Class source
  Plain text file LockExpirationInterface.php Class Class source
  Plain text file LockInformationProviderInterface.php Class Class source
  Plain text file LockInterface.php Class Class source
  Plain text file MemcachedLock.php Class Class source
  Plain text file MemcacheLock.php Class Class source
  Plain text file MySQLPDOLock.php Class Class source
  Plain text file PhpRedisLock.php Class Class source
  Plain text file PredisRedisLock.php Class Class source
  Plain text file ResolvedHostnameLo...rmationProvider.php Class Class source

  Files folder image Files (45)  /  tests  
File Role Description
Files folder imageLock (1 file, 1 directory)
Files folder imageMock (8 files)
  Plain text file AbstractTest.php Class Class source
  Plain text file MutexFabricTest.php Class Class source
  Plain text file MutexLocksTest.php Class Class source
  Plain text file MutexTest.php Class Class source

  Files folder image Files (45)  /  tests  /  Lock  
File Role Description
Files folder imageFabric (3 files)
  Plain text file LockTest.php Class Class source

  Files folder image Files (45)  /  tests  /  Lock  /  Fabric  
File Role Description
  Plain text file LockFabricWithExpirationInterface.php Class Class source
  Plain text file MemcachedLockFabric.php Class Class source
  Plain text file MemcacheLockFabric.php Class Class source

  Files folder image Files (45)  /  tests  /  Mock  
File Role Description
  Plain text file MockLock.php Class Class source
  Plain text file MockMemcache.php Class Class source
  Plain text file MockMemcached.php Class Class source
  Plain text file MockPDO.php Class Class source
  Plain text file MockPDOStatement.php Class Class source
  Plain text file MockPhpRedisClient.php Class Class source
  Plain text file MockPredisClient.php Class Class source
  Plain text file PermanentServiceInterface.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:176
This week:0
All time:8,767
This week:206Up
User Comments (1)