for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace LibraryCatalog\Service\Repository;
use Predis\Client;
trait RedisTrait
{
/**
* @param Client $predis
* @param string $key
* @param int $ttl
*/
public function lock(Client $predis, string $key, int $ttl): void
while ($predis->get($key)) {
usleep(50000);
}
$predis->setex($key, $ttl, true);
public function unlock(Client $predis, string $key): void
$predis->del($key);
* @param \Closure $closure
* @throws \Throwable
* @return mixed
public function transaction(Client $predis, string $key, int $ttl, \Closure $closure)
$this->lock($predis, $key, $ttl);
try {
$res = $closure();
} catch (\Exception | \Throwable $e) {
$this->unlock($predis, $key);
throw $e;
// Unlock in correct flow.
return $res;