PHP Classes
elePHPant
Icontem

Ninja Mutex: Lock resources to prevent simultaneous accesses

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (29)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2015-06-13 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 154 All time: 7,740 This week: 1,259Up
Version License PHP version Categories  
ninja-mutex 0.4.2MIT/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
Name: Kamil Dziedzic <contact>
Classes: 1 package by
Country: Poland Poland
Age: 30
All time rank: 378194 in Poland Poland
Week rank: 1592 Up44 in Poland Poland Up

Details provided by the author  
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![GitHub version](https://badge.fury.io/gh/arvenil%2Fninja-mutex.svg)](http://badge.fury.io/gh/arvenil%2Fninja-mutex)
[![Build Status](https://travis-ci.org/arvenil/ninja-mutex.svg?branch=master)](https://travis-ci.org/arvenil/ninja-mutex)
[![HHVM Status](http://hhvm.h4cc.de/badge/arvenil/ninja-mutex.svg)](http://hhvm.h4cc.de/package/arvenil/ninja-mutex)
[![Code Climate](https://codeclimate.com/github/arvenil/ninja-mutex/badges/gpa.svg)](https://codeclimate.com/github/arvenil/ninja-mutex)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/15c5c748-f8d8-4b56-b536-a29a151aac6c/mini.png)](https://insight.sensiolabs.com/projects/15c5c748-f8d8-4b56-b536-a29a151aac6c)
[![Dependency Status](https://gemnasium.com/arvenil/ninja-mutex.svg)](https://gemnasium.com/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 setup it 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 adapter and setup it properly. For example if you choose flock implementation first you need to setup 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. You setup 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

and 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 console:

    phpunit

## Something doesn't work

Feel free to fork project, fix bugs and finally request for pull
  Files folder image Files  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Composer requires
Accessible without login Plain text file LICENSE Lic. License MIT
Accessible without login Plain text file phpunit.xml.dist Data phpunit configuration
Accessible without login Plain text file README.md Data Readme

  Files folder image Files  /  src  
File Role Description
Files folder imageNinjaMutex (4 files, 1 directory)

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

  Files folder image Files  /  src  /  NinjaMutex  /  Lock  
File Role Description
  Plain text file FlockLock.php Class Flock lock implementation
  Plain text file LockAbstract.php Class Abstract lock implementation
  Plain text file LockInterface.php Class Lock interface
  Plain text file MemcachedLock.php Class Memcached lock implementation
  Plain text file MemcacheLock.php Class Memcache lock implementation
  Plain text file MemcacheLockAbstract.php Class Memcache lock abstract class
  Plain text file MySqlLock.php Class Mysql lock implementation
  Plain text file PredisRedisLock.php Class Predis lock implementation

  Files folder image Files  /  tests  
File Role Description
Files folder imageNinjaMutex (4 files, 2 directories)
  Accessible without login Plain text file bootstrap.php Test phpunit bootstrap

  Files folder image Files  /  tests  /  NinjaMutex  
File Role Description
Files folder imageLock (1 file)
Files folder imageMock (7 files)
  Accessible without login Plain text file AbstractTest.php Test Test
  Accessible without login Plain text file MutexFabricTest.php Test Test
  Plain text file MutexLocksTest.php Class Class source
  Accessible without login Plain text file MutexTest.php Test Test

  Files folder image Files  /  tests  /  NinjaMutex  /  Lock  
File Role Description
  Accessible without login Plain text file LockTest.php Test Test

  Files folder image Files  /  tests  /  NinjaMutex  /  Mock  
File Role Description
  Plain text file MockLock.php Class Class source
  Accessible without login Plain text file MockMemcache.php Test Test
  Accessible without login Plain text file MockMemcached.php Test Test
  Accessible without login Plain text file MockPDO.php Test Test
  Accessible without login Plain text file MockPDOStatement.php Test Test
  Accessible without login Plain text file MockPredisClient.php Test Test
  Plain text file PermanentServiceInterface.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%Total:154All time:7,740
 This week:0This week:1,259Up
 User Comments (1)