2017 © Pedro Peláez
 

library ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

image

arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  • Sunday, October 8, 2017
  • by arvenil
  • Repository
  • 11 Watchers
  • 147 Stars
  • 345,348 Installations
  • PHP
  • 20 Dependents
  • 1 Suggesters
  • 24 Forks
  • 5 Open issues
  • 11 Versions
  • 10 % Grown

The README.md

License PHP Version, (*1)

Build, (*2)

Coverage Quality Maintainability Grade Total Downloads, (*3)

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., (*4)

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:, (*5)

<?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!, (*6)

<?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:, (*7)

wget -nc http://getcomposer.org/composer.phar

Add dependency to your project:, (*8)

php composer.phar require arvenil/ninja-mutex:*

Running tests

Tests require vfsStream to work. To install it, simply run in project dir:, (*9)

wget -nc http://getcomposer.org/composer.phar && php composer.phar install --dev

To run tests type in a console:, (*10)

vendor/bin/phpunit

Something doesn't work

Feel free to fork project, fix bugs and finally request for pull, (*11)

The Versions

08/10 2017

dev-master

9999999-dev https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

07/10 2016

dev-travis-preinstalled-composer

dev-travis-preinstalled-composer https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

07/10 2016

dev-composer-autoload-dev

dev-composer-autoload-dev https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

31/05 2016

0.6.0

0.6.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

10/09 2015

0.5.1

0.5.1.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

13/06 2015

0.5.0

0.5.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

01/10 2014

0.4.1

0.4.1.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

30/09 2014

0.4.0

0.4.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

redis memcache memcached mysql lock locking mutex flock

15/09 2013

0.3.0

0.3.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached, mysql or redis for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Kamil Dziedzic

memcache memcached mysql lock locking mutex flock

25/03 2013

0.2.0

0.2.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache, memcached or mysql for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

  • mikey179/vfsstream v1.1.0
  • ext-memcache *
  • ext-memcached *
  • ext-pdo_mysql *

by Kamil Dziedzic

memcache memcached mysql lock locking mutex flock

21/02 2013

0.1.0

0.1.0.0 https://github.com/arvenil/ninja-mutex

Simple to use mutex implementation that can use flock, memcache or mysql for locking

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

  • mikey179/vfsstream v1.1.0

by Kamil Dziedzic

memcache mysql mutex flock