@@ -26,16 +26,10 @@ class MemcachedStore implements PersistingStoreInterface
26
26
{
27
27
use ExpiringStoreTrait;
28
28
29
- /**
30
- * @internal
31
- */
32
29
private const NS_SEPARATOR = ': ' ;
33
-
34
30
private bool $ useExtendedReturn ;
35
-
36
31
private string $ namespace = '' ;
37
32
38
-
39
33
public static function isSupported (): bool
40
34
{
41
35
return \extension_loaded ('memcached ' );
@@ -64,7 +58,7 @@ public function save(Key $key): void
64
58
{
65
59
$ token = $ this ->getUniqueToken ($ key );
66
60
$ 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 ))) {
68
62
// the lock is already acquired. It could be us. Let's try to put off.
69
63
$ this ->putOffExpiration ($ key , $ this ->initialTtl );
70
64
}
@@ -88,7 +82,7 @@ public function putOffExpiration(Key $key, float $ttl): void
88
82
$ key ->reduceLifetime ($ ttl );
89
83
// Could happens when we ask a putOff after a timeout but in luck nobody steal the lock
90
84
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 )) {
92
86
return ;
93
87
}
94
88
@@ -101,7 +95,7 @@ public function putOffExpiration(Key $key, float $ttl): void
101
95
throw new LockConflictedException ();
102
96
}
103
97
104
- if (!$ this ->memcached ->cas ($ cas , $ this ->namespace . $ key , $ token , $ ttl )) {
98
+ if (!$ this ->memcached ->cas ($ cas , $ this ->namespace . $ key , $ token , $ ttl )) {
105
99
throw new LockConflictedException ();
106
100
}
107
101
@@ -120,18 +114,18 @@ public function delete(Key $key): void
120
114
}
121
115
122
116
// 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 )) {
124
118
// Someone steal our lock. It does not belongs to us anymore. Nothing to do.
125
119
return ;
126
120
}
127
121
128
122
// 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 );
130
124
}
131
125
132
126
public function exists (Key $ key ): bool
133
127
{
134
- return $ this ->memcached ->get ($ this ->namespace . $ key ) === $ this ->getUniqueToken ($ key );
128
+ return $ this ->memcached ->get ($ this ->namespace . $ key ) === $ this ->getUniqueToken ($ key );
135
129
}
136
130
137
131
private function getUniqueToken (Key $ key ): string
@@ -147,7 +141,7 @@ private function getUniqueToken(Key $key): string
147
141
private function getValueAndCas (Key $ key ): array
148
142
{
149
143
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 );
151
145
if (\Memcached::GET_ERROR_RETURN_VALUE === $ extendedReturn ) {
152
146
return [$ extendedReturn , 0.0 ];
153
147
}
@@ -156,7 +150,7 @@ private function getValueAndCas(Key $key): array
156
150
}
157
151
158
152
$ cas = 0.0 ;
159
- $ value = $ this ->memcached ->get ($ this ->namespace . $ key , null , $ cas );
153
+ $ value = $ this ->memcached ->get ($ this ->namespace . $ key , null , $ cas );
160
154
161
155
return [$ value , $ cas ];
162
156
}
0 commit comments