Skip to content

Commit 2219af1

Browse files
committed
📚 Updated README and examples
1 parent 00ad8d8 commit 2219af1

3 files changed

Lines changed: 18 additions & 25 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ Asynchronous [Redlock](https://redis.io/topics/distlock) algorithm implementatio
1010

1111
## Quickstart
1212

13-
Once [installed](#installation), you can incorporate Redlock in your projects by instantiating its _Custodian_; this entity is responsible for lock orchestration and it requires access to your process's event loop as well as to a Redis client object instance, e.g.
13+
Once [installed](#installation), you can incorporate Redlock in your projects by instantiating its _Custodian_; this entity is responsible for lock orchestration and it requires access to a Redis client object instance, e.g.
1414

1515
```php
1616
/* Instantiate prerequisites */
17-
$loop = \React\EventLoop\Factory::create();
1817
$factory = new \Clue\React\Redis\Factory($loop);
1918
$client = $factory->createLazyClient('127.0.0.1');
2019

2120
/* Instantiate our lock custodian */
22-
$custodian = new \RTCKit\React\Redlock\Custodian($loop, $client);
21+
$custodian = new \RTCKit\React\Redlock\Custodian($client);
2322
```
2423

2524
#### Acquiring locks
@@ -106,16 +105,16 @@ composer install
106105
Then, go to the project root and run:
107106

108107
```bash
109-
php -d memory_limit=-1 ./vendor/bin/phpunit
108+
php -d memory_limit=-1 ./vendor/bin/phpunit -c ./etc/phpunit.xml.dist
110109
```
111110

112111
### Static Analysis
113112

114113
In order to ensure high code quality, Redlock uses [PHPStan](https://github.com/phpstan/phpstan) and [Psalm](https://github.com/vimeo/psalm):
115114

116115
```sh
117-
php -d memory_limit=-1 ./vendor/bin/phpstan analyse -n -vvv --ansi --level=max src
118-
php -d memory_limit=-1 ./vendor/bin/psalm --show-info=true
116+
php -d memory_limit=-1 ./vendor/bin/phpstan analyse -c ./etc/phpstan.neon -n -vvv --ansi --level=max src
117+
php -d memory_limit=-1 ./vendor/bin/psalm --config=./etc/psalm.xml --show-info=true
119118
```
120119

121120
## License

examples/01-basic.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require(__DIR__ . '/../vendor/autoload.php');
1010

1111
use Clue\React\Redis\Factory as RedisFactory;
12-
use React\EventLoop\Factory as LoopFactory;
12+
use React\EventLoop\Loop;
1313
use React\Promise\PromiseInterface;
1414
use RTCKit\React\Redlock\Custodian;
1515
use RTCKit\React\Redlock\Lock;
@@ -23,14 +23,13 @@
2323
}
2424

2525
/* Instantiate prerequisites */
26-
$loop = LoopFactory::create();
27-
$factory = new RedisFactory($loop);
26+
$factory = new RedisFactory(Loop::get());
2827
$client = $factory->createLazyClient($host);
2928

3029
/* Instantiate our lock custodian */
31-
$custodian = new Custodian($loop, $client);
30+
$custodian = new Custodian($client);
3231

33-
$loop->addTimer(0.001, function () use ($custodian, $loop): void {
32+
Loop::addTimer(0.001, function () use ($custodian): void {
3433
$custodian->acquire('01-basic', 1)
3534
->then(function (?Lock $lock) use ($custodian): PromiseInterface {
3635
if (is_null($lock)) {
@@ -58,11 +57,9 @@
5857
->otherwise(function (Throwable $t): void {
5958
echo "Something bad happened:" . PHP_EOL . " > " . $t->getMessage() . PHP_EOL;
6059
})
61-
->always(function() use ($loop): void {
62-
$loop->stop();
60+
->always(function(): void {
61+
Loop::stop();
6362

6463
echo "Bye!" . PHP_EOL;
6564
});
6665
});
67-
68-
$loop->run();

examples/02-spin.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require(__DIR__ . '/../vendor/autoload.php');
1010

1111
use Clue\React\Redis\Factory as RedisFactory;
12-
use React\EventLoop\Factory as LoopFactory;
12+
use React\EventLoop\Loop;
1313
use React\Promise\PromiseInterface;
1414
use RTCKit\React\Redlock\Custodian;
1515
use RTCKit\React\Redlock\Lock;
@@ -22,15 +22,14 @@
2222
}
2323

2424
/* Instantiate prerequisites */
25-
$loop = LoopFactory::create();
26-
$factory = new RedisFactory($loop);
25+
$factory = new RedisFactory(Loop::get());
2726
$client = $factory->createLazyClient($host);
2827

2928
/* Instantiate our lock custodian */
30-
$custodian = new Custodian($loop, $client);
29+
$custodian = new Custodian($client);
3130

3231
/* First routine to acquire the lock */
33-
$loop->addTimer(0.001, function () use ($custodian): void {
32+
Loop::addTimer(0.001, function () use ($custodian): void {
3433
/* Lock '02-spin' for 5 seconds */
3534
$custodian->acquire('02-spin', 5)
3635
->then(function (?Lock $lock): void {
@@ -46,7 +45,7 @@
4645
});
4746

4847
/* Hopeful routine, attempting to eventually acquire the lock */
49-
$loop->addTimer(1, function ($timer) use ($custodian, $loop): void {
48+
Loop::addTimer(1, function ($timer) use ($custodian): void {
5049
/* Attempt to lock '02-spin', trying up to 7 times with a one second pause between retries.
5150
Once secured, hold the lock for 10 seconds. */
5251
$custodian->spin(7, 1, '02-spin', 10)
@@ -60,11 +59,9 @@
6059
->otherwise(function (Throwable $t): void {
6160
echo "[hopeful] Something bad happened:" . PHP_EOL . " > " . $t->getMessage() . PHP_EOL;
6261
})
63-
->always(function() use ($loop): void {
64-
$loop->stop();
62+
->always(function(): void {
63+
Loop::stop();
6564

6665
echo "Bye!" . PHP_EOL;
6766
});
6867
});
69-
70-
$loop->run();

0 commit comments

Comments
 (0)