2017 © Pedro Peláez
 

library mutex

PHP Mutex Library

image

denismitr/mutex

PHP Mutex Library

  • Tuesday, November 21, 2017
  • by denismitr
  • Repository
  • 1 Watchers
  • 1 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHP Mutex Library

Author

Denis Mitrofanov, (*1)

Installation

composer require denismitr/mutex

Usage

Initialization with factory: - File lock, (*2)

$lock = MutexFactory::fileLock(__FILE__); // or some other file name like /tmp/some-id
  • Semaphore lock (linux only)
$lock = MutexFactory::semaphoreLock(__FILE__); // or some other file name like /tmp/some-id
  • PRedis lock
$this->redis = new Client([
    'host' => 'localhost',
    'port' => 6379,
    'database' => 0,
]);

$this->lock = MutexFactory::pRedisLock($this->redis, "some-key", 20);

This far only these types of locks are supported, (*3)

Using the lock instances, (*4)

$lock->acquire();

// Do some critical stuff here

$lock->release();

With closures, (*5)

$lock->safe(function() {
    // Lock will be acuqired and released automatically

    // Do some critical stuff safely
});

Performing a check first, (*6)

$lock->try(function() use ($room, $from, $to) {
    // e.g
    return $room->isFree($from, $to);
})->then(function() use ($room, $from, $to) {
    // e.g.
    // Lock is aquired automatically

    $room->book($from, $to);
})->fail(function() use ($user) {
    // this callback will fire if the condition in try closure fails
    // e.g.
    $user->notify("Room is not available for requested time period.");
});

Looping in the safe, locked mode, (*7)

$lock->loop($timeoutInSeconds, function($loop, $i) ($user, $ads) {
    // lock is acquired and released automatically when loop is done
    // e.g. send out only 10 ads to user friends

    // Laravel collections example
    $user->friends->each->notify($adds->random());

    if ($i >= 10) {
        $loop->stop();
    }
});

The Versions

21/11 2017

dev-master

9999999-dev

PHP Mutex Library

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

redis predis lock mutex semaphore flock

21/11 2017

v0.1

0.1.0.0

PHP Mutex Library

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

redis predis lock mutex semaphore flock