Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kelunik/redis-mutex
Mutex implementation using Redis.
https://github.com/kelunik/redis-mutex
amphp mutex redis redis-mutex
Last synced: 3 months ago
JSON representation
Mutex implementation using Redis.
- Host: GitHub
- URL: https://github.com/kelunik/redis-mutex
- Owner: kelunik
- Created: 2015-05-31T00:07:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-20T22:07:53.000Z (about 6 years ago)
- Last Synced: 2024-10-10T18:50:51.683Z (4 months ago)
- Topics: amphp, mutex, redis, redis-mutex
- Language: PHP
- Homepage:
- Size: 20.5 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Redis Mutex [![Build Status](https://travis-ci.org/kelunik/redis-mutex.svg?branch=master)](https://travis-ci.org/kelunik/redis-mutex)
Distributed mutual exclusion built upon the [Amp concurrency framework](https://github.com/amphp/amp) and [Redis](http://redis.io).
## Basic Example
```php
$mutex = new Mutex(...);// ...
try {
$token = bin2hex(random_bytes(16));
yield $mutex->lock($sessionId, $token);// Code here will only be executed in one client at a time.
// If it takes longer than your specified TTL, you have to
// renew the lock, see next example.yield $mutex->unlock($sessionId, $token);
} catch (MutexException $e) {
// ...
}
```## Renew Example
```php
$mutex = new Mutex(...);
$locks = [];Loop::repeat(1000, function () use ($mutex, $locks) {
foreach ($locks as $id => $token) {
$mutex->renew($id, $token);
}
});// ...
try {
$token = bin2hex(random_bytes(16));
yield $mutex->lock($sessionId, $token);
$locks[$sessionId] = $token;// Code here will only be executed in one client at a time.
// Your lock will automatically be renewed by the reactor
// repeat above. Don't do blocking things here (you should never
// do that with Amp anyway), otherwise the reactor will not
// be able to schedule the renewal.unset($locks[$sessionId]);
yield $mutex->unlock($sessionId, $token);
} catch (MutexException $e) {
// ...
}
```