Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 788c1c1

Browse filesBrowse files
committed
[Lock] Add namespace support to Redis & Memcache lock stores
1 parent c186d5e commit 788c1c1
Copy full SHA for 788c1c1

File tree

2 files changed

+14
-25
lines changed
Filter options

2 files changed

+14
-25
lines changed

‎src/Symfony/Component/Lock/Store/MemcachedStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/MemcachedStore.php
+8-14
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,10 @@ class MemcachedStore implements PersistingStoreInterface
2626
{
2727
use ExpiringStoreTrait;
2828

29-
/**
30-
* @internal
31-
*/
3229
private const NS_SEPARATOR = ':';
33-
3430
private bool $useExtendedReturn;
35-
3631
private string $namespace = '';
3732

38-
3933
public static function isSupported(): bool
4034
{
4135
return \extension_loaded('memcached');
@@ -64,7 +58,7 @@ public function save(Key $key): void
6458
{
6559
$token = $this->getUniqueToken($key);
6660
$key->reduceLifetime($this->initialTtl);
67-
if (!$this->memcached->add($this->namespace . $key, $token, (int) ceil($this->initialTtl))) {
61+
if (!$this->memcached->add($this->namespace.$key, $token, (int) ceil($this->initialTtl))) {
6862
// the lock is already acquired. It could be us. Let's try to put off.
6963
$this->putOffExpiration($key, $this->initialTtl);
7064
}
@@ -88,7 +82,7 @@ public function putOffExpiration(Key $key, float $ttl): void
8882
$key->reduceLifetime($ttl);
8983
// Could happens when we ask a putOff after a timeout but in luck nobody steal the lock
9084
if (\Memcached::RES_NOTFOUND === $this->memcached->getResultCode()) {
91-
if ($this->memcached->add($this->namespace . $key, $token, $ttl)) {
85+
if ($this->memcached->add($this->namespace.$key, $token, $ttl)) {
9286
return;
9387
}
9488

@@ -101,7 +95,7 @@ public function putOffExpiration(Key $key, float $ttl): void
10195
throw new LockConflictedException();
10296
}
10397

104-
if (!$this->memcached->cas($cas, $this->namespace . $key, $token, $ttl)) {
98+
if (!$this->memcached->cas($cas, $this->namespace.$key, $token, $ttl)) {
10599
throw new LockConflictedException();
106100
}
107101

@@ -120,18 +114,18 @@ public function delete(Key $key): void
120114
}
121115

122116
// To avoid concurrency in deletion, the trick is to extends the TTL then deleting the key
123-
if (!$this->memcached->cas($cas, $this->namespace . $key, $token, 2)) {
117+
if (!$this->memcached->cas($cas, $this->namespace.$key, $token, 2)) {
124118
// Someone steal our lock. It does not belongs to us anymore. Nothing to do.
125119
return;
126120
}
127121

128122
// Now, we are the owner of the lock for 2 more seconds, we can delete it.
129-
$this->memcached->delete($this->namespace . $key);
123+
$this->memcached->delete($this->namespace.$key);
130124
}
131125

132126
public function exists(Key $key): bool
133127
{
134-
return $this->memcached->get($this->namespace . $key) === $this->getUniqueToken($key);
128+
return $this->memcached->get($this->namespace.$key) === $this->getUniqueToken($key);
135129
}
136130

137131
private function getUniqueToken(Key $key): string
@@ -147,7 +141,7 @@ private function getUniqueToken(Key $key): string
147141
private function getValueAndCas(Key $key): array
148142
{
149143
if ($this->useExtendedReturn ??= version_compare(phpversion('memcached'), '2.9.9', '>')) {
150-
$extendedReturn = $this->memcached->get($this->namespace . $key, null, \Memcached::GET_EXTENDED);
144+
$extendedReturn = $this->memcached->get($this->namespace.$key, null, \Memcached::GET_EXTENDED);
151145
if (\Memcached::GET_ERROR_RETURN_VALUE === $extendedReturn) {
152146
return [$extendedReturn, 0.0];
153147
}
@@ -156,7 +150,7 @@ private function getValueAndCas(Key $key): array
156150
}
157151

158152
$cas = 0.0;
159-
$value = $this->memcached->get($this->namespace . $key, null, $cas);
153+
$value = $this->memcached->get($this->namespace.$key, null, $cas);
160154

161155
return [$value, $cas];
162156
}

‎src/Symfony/Component/Lock/Store/RedisStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/RedisStore.php
+6-11
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ class RedisStore implements SharedLockStoreInterface
3232
use ExpiringStoreTrait;
3333

3434
private const NO_SCRIPT_ERROR_MESSAGE_PREFIX = 'NOSCRIPT';
35-
/**
36-
* @internal
37-
*/
3835
private const NS_SEPARATOR = ':';
39-
4036
private bool $supportTime;
41-
4237
private string $namespace = '';
4338

4439
/**
@@ -98,7 +93,7 @@ public function save(Key $key): void
9893
';
9994

10095
$key->reduceLifetime($this->initialTtl);
101-
if (!$this->evaluate($script, $this->namespace . $key, [microtime(true), $this->getUniqueToken($key), (int) ceil($this->initialTtl * 1000)])) {
96+
if (!$this->evaluate($script, $this->namespace.$key, [microtime(true), $this->getUniqueToken($key), (int) ceil($this->initialTtl * 1000)])) {
10297
throw new LockConflictedException();
10398
}
10499

@@ -138,7 +133,7 @@ public function saveRead(Key $key): void
138133
';
139134

140135
$key->reduceLifetime($this->initialTtl);
141-
if (!$this->evaluate($script, $this->namespace . $key, [microtime(true), $this->getUniqueToken($key), (int) ceil($this->initialTtl * 1000)])) {
136+
if (!$this->evaluate($script, $this->namespace.$key, [microtime(true), $this->getUniqueToken($key), (int) ceil($this->initialTtl * 1000)])) {
142137
throw new LockConflictedException();
143138
}
144139

@@ -178,7 +173,7 @@ public function putOffExpiration(Key $key, float $ttl): void
178173
';
179174

180175
$key->reduceLifetime($ttl);
181-
if (!$this->evaluate($script, $this->namespace . $key, [microtime(true), $this->getUniqueToken($key), (int) ceil($ttl * 1000)])) {
176+
if (!$this->evaluate($script, $this->namespace.$key, [microtime(true), $this->getUniqueToken($key), (int) ceil($ttl * 1000)])) {
182177
throw new LockConflictedException();
183178
}
184179

@@ -212,7 +207,7 @@ public function delete(Key $key): void
212207
return true
213208
';
214209

215-
$this->evaluate($script, $this->namespace . $key, [$this->getUniqueToken($key)]);
210+
$this->evaluate($script, $this->namespace.$key, [$this->getUniqueToken($key)]);
216211
}
217212

218213
public function exists(Key $key): bool
@@ -238,7 +233,7 @@ public function exists(Key $key): bool
238233
return false
239234
';
240235

241-
return (bool) $this->evaluate($script, $this->namespace . $key, [microtime(true), $this->getUniqueToken($key)]);
236+
return (bool) $this->evaluate($script, $this->namespace.$key, [microtime(true), $this->getUniqueToken($key)]);
242237
}
243238

244239
private function evaluate(string $script, string $resource, array $args): mixed
@@ -336,7 +331,7 @@ private function getNowCode(): string
336331
return 1
337332
';
338333
try {
339-
$this->supportTime = 1 === $this->evaluate($script, $this->namespace . 'symfony_check_support_time', []);
334+
$this->supportTime = 1 === $this->evaluate($script, $this->namespace.'symfony_check_support_time', []);
340335
} catch (LockStorageException $e) {
341336
if (!str_contains($e->getMessage(), 'commands not allowed after non deterministic')
342337
&& !str_contains($e->getMessage(), 'is not allowed from script script')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.