2017 © Pedro Peláez
 

library redlock-php

Redis distributed locks in PHP

image

kohver/redlock-php

Redis distributed locks in PHP

  • Wednesday, August 31, 2016
  • by kohver
  • Repository
  • 0 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 113 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

redlock-php - Redis distributed locks in PHP, (*1)

Based on Redlock-rb by Salvatore Sanfilippo, (*2)

This library implements the Redis-based distributed lock manager algorithm described in this blog post., (*3)

To create a lock manager:, (*4)


$RedLock = new RedLock([ new Client(['host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0.01]), new Client(['host' => '127.0.0.1', 'port' => 6380, 'timeout' => 0.01]), new Client(['host' => '127.0.0.1', 'port' => 6381, 'timeout' => 0.01]), ]);

To acquire a lock:, (*5)


$lock = $RedLock->lock('my_resource_name', 1000);

Where the resource name is an unique identifier of what you are trying to lock and 1000 is the number of milliseconds for the validity time., (*6)

If the lock was not acquired LockTimeoutException will be thrown, otherwise an instance of Lock is returned, having three methods:, (*7)

  • getValidity, an integer representing the number of milliseconds the lock will be valid.
  • getResource, the name of the locked resource as specified by the user.
  • getToken, a random token value which is used to safe reclaim the lock.

To release a lock:, (*8)


$RedLock->unlock($lock)

It is possible to setup the number of retries (by default 3) and the retry delay (by default 200 milliseconds) used to acquire the lock., (*9)

The retry delay is actually chosen at random between $retryDelay / 2 milliseconds and the specified $retryDelay value., (*10)

Disclaimer: As stated in the original antirez's version, this code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments., (*11)

The Versions

31/08 2016

dev-master

9999999-dev

Redis distributed locks in PHP

  Sources   Download

The Requires

 

by Ronny Lopez
by Artyom Kohver