2017 © Pedro Peláez
 

library redis-counting-semaphore

Distributed counting semaphore implementation for redis

image

code-orange/redis-counting-semaphore

Distributed counting semaphore implementation for redis

  • Thursday, April 19, 2018
  • by timvdalen
  • Repository
  • 2 Watchers
  • 3 Stars
  • 111 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 16 % Grown

The README.md

Redis counting semaphore Latest Stable Version Total Downloads License composer.lock

redis-counting-semaphore is a package with a counting semaphore implementation for PHP. It uses redis as a central broker., (*1)

Installation

To install redis-counting-semaphore with composer:, (*2)

composer require code-orange/redis-counting-semaphore

Usage

First, make sure you have a Predis connection instance., (*3)

You can create and attempt to obtain a semaphore like so:, (*4)

<?php
use CodeOrange\RedisCountingSemaphore\Semaphore;

$client = new Predis\Client();

// Create a counting semaphore with a limit of 3
$sem = new Semaphore($client, 'semaphore-name', 3);
if ($sem->acquire(0.1, 10)) {
    // Obtained the semaphore
    use_limited_resource();
    $sem->release();
} else {
    // We weren't able to get a semaphore, even though we tried 10 times
    // And slept for 0.1 seconds in between tries
}

API

/**
 * Semaphore constructor.
 * @param Client $client Predis client with an open connection
 * @param string $name Name of the semaphore
 * @param int $limit The amount of resources this semaphore protects
 * @param int $timeout Timeout of an acquired semaphore, in seconds
 */
public Semaphore(Client $client, $name, $limit = 1, $timeout = 10);

/**
 * Try to acquire a semaphore
 *
 * @param float $sleep Number of seconds to sleep between retries. If null, this function will not retry but return immediately.
 * @param int $retries Number of times to retry before giving up
 * @return bool Whether or not the semaphore was acquired correctly
 */
public function acquire($sleep = null, $retries = null);

/**
 * Release this semaphore
 *
 * @return void
 */
public function release();

/**
 * Refresh the semaphore
 *
 * @return bool Whether or not we still have the semaphore
 */
public function refresh();

The Versions

19/04 2018

dev-master

9999999-dev

Distributed counting semaphore implementation for redis

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tim van Dalen

19/04 2018

v1.1

1.1.0.0

Distributed counting semaphore implementation for redis

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tim van Dalen

14/03 2018

v1.0.0

1.0.0.0

Distributed counting semaphore implementation for redis

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Tim van Dalen

13/03 2018

v0.1

0.1.0.0

Distributed counting semaphore implementation for redis

  Sources   Download

Apache-2.0

The Requires

 

by Tim van Dalen