Skip to content

Commit 5167551

Browse files
committed
impl. Redis
1 parent 382bc3b commit 5167551

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/mutex/PHPRedisMutex.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ public function __construct(array $redisAPIs, string $name, float $timeout = 3)
4545
*/
4646
protected function add($redisAPI, string $key, string $value, float $expire): bool
4747
{
48+
$expireMillis = (int) ceil($expire * 1000);
49+
4850
/** @var \Redis $redisAPI */
4951
try {
5052
// Will set the key, if it doesn't exist, with a ttl of $expire seconds
51-
return $redisAPI->set($key, $value, ['nx', 'ex' => $expire]);
53+
return $redisAPI->set($key, $value, ['nx', 'px' => $expireMillis]);
5254
} catch (RedisException $e) {
5355
$message = sprintf(
5456
"Failed to acquire lock for key '%s'",

src/mutex/PredisMutex.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ public function __construct(array $clients, string $name, float $timeout = 3)
3535
*/
3636
protected function add($redisAPI, string $key, string $value, float $expire): bool
3737
{
38+
$expireMillis = (int) ceil($expire * 1000);
39+
3840
/** @var ClientInterface $redisAPI */
3941
try {
40-
return $redisAPI->set($key, $value, 'EX', $expire, 'NX') !== null;
42+
return $redisAPI->set($key, $value, 'PX', $expireMillis, 'NX') !== null;
4143
} catch (PredisException $e) {
4244
$message = sprintf(
4345
"Failed to acquire lock for key '%s'",

tests/mutex/PredisMutexTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testAddFailsToSetKey()
5353
{
5454
$this->client->expects($this->atLeastOnce())
5555
->method('set')
56-
->with('lock_test', $this->isType('string'), 'EX', 4, 'NX')
56+
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
5757
->willReturn(null);
5858

5959
$this->logger->expects($this->never())
@@ -75,7 +75,7 @@ public function testAddErrors()
7575
{
7676
$this->client->expects($this->atLeastOnce())
7777
->method('set')
78-
->with('lock_test', $this->isType('string'), 'EX', 4, 'NX')
78+
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
7979
->willThrowException($this->createMock(PredisException::class));
8080

8181
$this->logger->expects($this->once())
@@ -95,7 +95,7 @@ public function testWorksNormally()
9595
{
9696
$this->client->expects($this->atLeastOnce())
9797
->method('set')
98-
->with('lock_test', $this->isType('string'), 'EX', 4, 'NX')
98+
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
9999
->willReturnSelf();
100100

101101
$this->client->expects($this->once())
@@ -119,7 +119,7 @@ public function testEvalScriptFails()
119119
{
120120
$this->client->expects($this->atLeastOnce())
121121
->method('set')
122-
->with('lock_test', $this->isType('string'), 'EX', 4, 'NX')
122+
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
123123
->willReturnSelf();
124124

125125
$this->client->expects($this->once())

0 commit comments

Comments
 (0)