Skip to content

Commit d5e9996

Browse files
Switch to string tokens as described in the Redis SET documentation.
1 parent b2af0ca commit d5e9996

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

classes/mutex/PHPRedisMutex.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,21 @@ public function __construct(array $redisAPIs, string $name, int $timeout = 3)
3636
{
3737
parent::__construct($redisAPIs, $name, $timeout);
3838
}
39-
40-
protected function add($redis, string $key, int $value, int $expire): bool
39+
40+
/**
41+
* @throws LockAcquireException
42+
*/
43+
protected function add($redisAPI, string $key, string $value, int $expire): bool
4144
{
42-
/** @var Redis $redis */
45+
/** @var Redis $redisAPI */
4346
try {
4447
// Will set the key, if it doesn't exist, with a ttl of $expire seconds
45-
return $redis->set($key, $value, ["nx", "ex" => $expire]);
48+
return $redisAPI->set($key, $value, ["nx", "ex" => $expire]);
4649
} catch (RedisException $e) {
4750
$message = sprintf(
4851
"Failed to acquire lock for key '%s' at %s",
4952
$key,
50-
$this->getRedisIdentifier($redis)
53+
$this->getRedisIdentifier($redisAPI)
5154
);
5255
throw new LockAcquireException($message, 0, $e);
5356
}

classes/mutex/PredisMutex.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ public function __construct(array $clients, string $name, int $timeout = 3)
3232
{
3333
parent::__construct($clients, $name, $timeout);
3434
}
35-
36-
protected function add($client, string $key, int $value, int $expire): bool
35+
36+
/**
37+
* @throws LockAcquireException
38+
*/
39+
protected function add($redisAPI, string $key, string $value, int $expire): bool
3740
{
38-
/** @var ClientInterface $client */
41+
/** @var ClientInterface $redisAPI */
3942
try {
40-
return $client->set($key, $value, "EX", $expire, "NX") !== null;
43+
return $redisAPI->set($key, $value, "EX", $expire, "NX") !== null;
4144
} catch (PredisException $e) {
4245
$message = sprintf(
4346
"Failed to acquire lock for key '%s' at %s",
4447
$key,
45-
$this->getRedisIdentifier($client)
48+
$this->getRedisIdentifier($redisAPI)
4649
);
4750
throw new LockAcquireException($message, 0, $e);
4851
}
@@ -68,6 +71,14 @@ protected function evalScript($client, string $script, int $numkeys, array $argu
6871
protected function getRedisIdentifier($client)
6972
{
7073
/** @var ClientInterface $client */
74+
75+
/*
76+
* Note that there is no __toString() on the \Predis\Connection\ConnectionInterface
77+
* class. Some classes such as \Predis\Connection\Aggregate\SentinelReplication don't
78+
* have a __toString() method.
79+
*
80+
* todo fix this
81+
*/
7182
return (string) $client->getConnection();
7283
}
7384
}

classes/mutex/RedisMutex.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public function setLogger(LoggerInterface $logger)
6767
$this->logger = $logger;
6868
}
6969

70-
/**
71-
* @SuppressWarnings(PHPMD)
72-
*/
7370
protected function acquire(string $key, int $expire): bool
7471
{
7572
// 1. This differs from the specification to avoid an overflow on 32-Bit systems.
@@ -78,7 +75,7 @@ protected function acquire(string $key, int $expire): bool
7875
// 2.
7976
$acquired = 0;
8077
$errored = 0;
81-
$this->token = \random_int(0, 2147483647);
78+
$this->token = \random_bytes(16);
8279
$exception = null;
8380
foreach ($this->redisAPIs as $redis) {
8481
try {
@@ -168,20 +165,18 @@ private function isMajority(int $count): bool
168165
{
169166
return $count > count($this->redisAPIs) / 2;
170167
}
171-
168+
172169
/**
173170
* Sets the key only if such key doesn't exist at the server yet.
174171
*
175-
* @param mixed $redisAPI The connected Redis API.
172+
* @param mixed $redisAPI The connected Redis API.
176173
* @param string $key The key.
177174
* @param string $value The value.
178-
* @param int $expire The TTL seconds.
175+
* @param int $expire The TTL seconds.
179176
*
180177
* @return bool True, if the key was set.
181-
* @throws LockAcquireException An unexpected error happened.
182-
* @internal
183178
*/
184-
abstract protected function add($redisAPI, string $key, int $value, int $expire): bool;
179+
abstract protected function add($redisAPI, string $key, string $value, int $expire): bool;
185180

186181
/**
187182
* @param mixed $redisAPI The connected Redis API.
@@ -191,7 +186,6 @@ abstract protected function add($redisAPI, string $key, int $value, int $expire)
191186
*
192187
* @return mixed The script result, or false if executing failed.
193188
* @throws LockReleaseException An unexpected error happened.
194-
* @internal
195189
*/
196190
abstract protected function evalScript($redisAPI, string $script, int $numkeys, array $arguments);
197191

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
},
6464
"suggest": {
6565
"ext-pnctl": "Enables locking with flock without busy waiting in CLI scripts.",
66-
"ext-sysvsem": "Enables locking using semaphores",
66+
"ext-sysvsem": "Enables locking using semaphores.",
67+
"ext-redis": "To use this library with the PHP Redis extension.",
6768
"predis/predis": "To use this library with predis."
6869
},
6970
"archive": {

tests/mutex/PredisMutexTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testAddFailsToSetKey()
4747
{
4848
$this->client->expects($this->atLeastOnce())
4949
->method("set")
50-
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
50+
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
5151
->willReturn(null);
5252

5353
$this->mutex->synchronized(
@@ -66,7 +66,7 @@ public function testAddErrors()
6666
{
6767
$this->client->expects($this->atLeastOnce())
6868
->method("set")
69-
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
69+
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
7070
->willThrowException($this->createMock(PredisException::class));
7171

7272
$this->mutex->synchronized(
@@ -80,12 +80,12 @@ public function testWorksNormally()
8080
{
8181
$this->client->expects($this->atLeastOnce())
8282
->method("set")
83-
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
83+
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
8484
->willReturnSelf();
8585

8686
$this->client->expects($this->once())
8787
->method("eval")
88-
->with($this->anything(), 1, "lock_test", $this->isType("integer"))
88+
->with($this->anything(), 1, "lock_test", $this->isType("string"))
8989
->willReturn(true);
9090

9191
$executed = false;
@@ -106,12 +106,12 @@ public function testEvalScriptFails()
106106
{
107107
$this->client->expects($this->atLeastOnce())
108108
->method("set")
109-
->with("lock_test", $this->isType("integer"), "EX", 4, "NX")
109+
->with("lock_test", $this->isType("string"), "EX", 4, "NX")
110110
->willReturnSelf();
111111

112112
$this->client->expects($this->once())
113113
->method("eval")
114-
->with($this->anything(), 1, "lock_test", $this->isType("integer"))
114+
->with($this->anything(), 1, "lock_test", $this->isType("string"))
115115
->willThrowException($this->createMock(PredisException::class));
116116

117117
$executed = false;

0 commit comments

Comments
 (0)