2017 © Pedro Peláez
 

library php-lock-redis

redis lock

image

texthtml/php-lock-redis

redis lock

  • Monday, February 19, 2018
  • by mathroc
  • Repository
  • 2 Watchers
  • 3 Stars
  • 9,531 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 5 Versions
  • 4 % Grown

The README.md

php-lock-redis

Build Status Latest Stable Version License Total Downloads Scrutinizer Code Quality, (*1)

php-lock-redis is an extension for php-lock that makes locking on resources easy on distributed system using Redis. It can be used instead of file base locking to lock operations on a distributed system., (*2)

Installation

With Composer :, (*3)

composer require texthtml/php-lock-redis

Usage

You can create an object that represent a lock on a resource. You can then try to acquire that lock by calling $lock->acquire(). If the lock fail it will throw an Exception (useful for CLI tools built with Symfony Console Components documentation). If the lock is acquired the program can continue., (*4)

Locking a ressource

use TH\RedisLock\RedisSimpleLockFactory;

$redisClient = new \Predis\Client;
$factory = new RedisSimpleLockFactory($redisClient);
$lock = $factory->create('lock identifier');

$lock->acquire();

// other processes that try to acquire a lock on 'lock identifier' will fail

// do some stuff

$lock->release();

// other processes can now acquire a lock on 'lock identifier'

Auto release

$lock->release() is called automatically when the lock is destroyed so you don't need to manually release it at the end of a script or if it goes out of scope., (*5)

use TH\RedisLock\RedisSimpleLockFactory;

function batch() {
    $redisClient = new \Predis\Client;
    $factory = new RedisSimpleLockFactory($redisClient);
    $lock = $factory->create('lock identifier');
    $lock->acquire();

    // lot of stuff
}

batch();

// the lock will be released here even if $lock->release() is not called in batch()

Limitations

Validity time

If a client crashes before releasing the lock (or forget to release it), no other clients would be able to acquire the lock again. To avoid Deadlock, RedisSimpleLock locks have a validity time at witch the lock key will expire. But be careful, if the operation is too long, another client might acquire the lock too., (*6)

Mutual exclusion

Because RedisSimpleLock does not implements the RedLock algorithm, it have a limitation : with a master slave replication, a race condition can occurs when the master crashes before the lock key is transmitted to the slave. In this case a second client could acquire the same lock., (*7)

The Versions

19/02 2018

dev-master

9999999-dev

redis lock

  Sources   Download

AGPLv3 AGPL-3.0-or-later

The Requires

 

The Development Requires

19/02 2018

dev-mathroc-patch-1

dev-mathroc-patch-1

redis lock

  Sources   Download

AGPL-3.0-or-later

The Requires

 

The Development Requires

21/04 2016

v1.1.1

1.1.1.0

redis lock

  Sources   Download

AGPLv3

The Requires

 

The Development Requires

19/11 2015

v1.1.0

1.1.0.0

redis lock

  Sources   Download

AGPLv3

The Requires

 

The Development Requires

18/10 2015

v1.0.0

1.0.0.0

redis lock

  Sources   Download

AGPLv3

The Requires

 

The Development Requires