Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

Commitd8c400b

Browse files
[Cache] Fix lazy Memcached connections
1 parent9db03c1 commitd8c400b

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

‎src/Symfony/Component/Cache/Traits/MemcachedTrait.php‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ trait MemcachedTrait
3030
);
3131

3232
private$client;
33+
private$lazyClient;
3334

3435
publicstaticfunctionisSupported()
3536
{
@@ -41,14 +42,18 @@ private function init(\Memcached $client, $namespace, $defaultLifetime)
4142
if (!static::isSupported()) {
4243
thrownewCacheException('Memcached >= 2.2.0 is required');
4344
}
44-
$opt =$client->getOption(\Memcached::OPT_SERIALIZER);
45-
if (\Memcached::SERIALIZER_PHP !==$opt && \Memcached::SERIALIZER_IGBINARY !==$opt) {
46-
thrownewCacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
45+
if (get_class($client) ==='Memcached') {
46+
$opt =$client->getOption(\Memcached::OPT_SERIALIZER);
47+
if (\Memcached::SERIALIZER_PHP !==$opt && \Memcached::SERIALIZER_IGBINARY !==$opt) {
48+
thrownewCacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
49+
}
50+
$this->maxIdLength -=strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));
51+
$this->client =$client;
52+
}else {
53+
$this->lazyClient =$client;
4754
}
48-
$this->maxIdLength -=strlen($client->getOption(\Memcached::OPT_PREFIX_KEY));
4955

5056
parent::__construct($namespace,$defaultLifetime);
51-
$this->client =$client;
5257
}
5358

5459
/**
@@ -191,7 +196,7 @@ protected function doSave(array $values, $lifetime)
191196
$lifetime +=time();
192197
}
193198

194-
return$this->checkResultCode($this->client->setMulti($values,$lifetime));
199+
return$this->checkResultCode($this->getClient()->setMulti($values,$lifetime));
195200
}
196201

197202
/**
@@ -201,7 +206,7 @@ protected function doFetch(array $ids)
201206
{
202207
$unserializeCallbackHandler =ini_set('unserialize_callback_func',__CLASS__.'::handleUnserializeCallback');
203208
try {
204-
return$this->checkResultCode($this->client->getMulti($ids));
209+
return$this->checkResultCode($this->getClient()->getMulti($ids));
205210
}catch (\Error$e) {
206211
thrownew \ErrorException($e->getMessage(),$e->getCode(),E_ERROR,$e->getFile(),$e->getLine());
207212
}finally {
@@ -214,7 +219,7 @@ protected function doFetch(array $ids)
214219
*/
215220
protectedfunctiondoHave($id)
216221
{
217-
returnfalse !==$this->client->get($id) ||$this->checkResultCode(\Memcached::RES_SUCCESS ===$this->client->getResultCode());
222+
returnfalse !==$this->getClient()->get($id) ||$this->checkResultCode(\Memcached::RES_SUCCESS ===$this->client->getResultCode());
218223
}
219224

220225
/**
@@ -223,7 +228,7 @@ protected function doHave($id)
223228
protectedfunctiondoDelete(array$ids)
224229
{
225230
$ok =true;
226-
foreach ($this->checkResultCode($this->client->deleteMulti($ids))as$result) {
231+
foreach ($this->checkResultCode($this->getClient()->deleteMulti($ids))as$result) {
227232
if (\Memcached::RES_SUCCESS !==$result && \Memcached::RES_NOTFOUND !==$result) {
228233
$ok =false;
229234
}
@@ -237,7 +242,7 @@ protected function doDelete(array $ids)
237242
*/
238243
protectedfunctiondoClear($namespace)
239244
{
240-
return$this->checkResultCode($this->client->flush());
245+
return$this->checkResultCode($this->getClient()->flush());
241246
}
242247

243248
privatefunctioncheckResultCode($result)
@@ -250,4 +255,24 @@ private function checkResultCode($result)
250255

251256
thrownewCacheException(sprintf('MemcachedAdapter client error: %s.',strtolower($this->client->getResultMessage())));
252257
}
258+
259+
/**
260+
* @return \Memcached
261+
*/
262+
privatefunctiongetClient()
263+
{
264+
if ($this->client) {
265+
return$this->client;
266+
}
267+
268+
$opt =$this->lazyClient->getOption(\Memcached::OPT_SERIALIZER);
269+
if (\Memcached::SERIALIZER_PHP !==$opt && \Memcached::SERIALIZER_IGBINARY !==$opt) {
270+
thrownewCacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
271+
}
272+
if ('' !==$prefix = (string)$this->lazyClient->getOption(\Memcached::OPT_PREFIX_KEY)) {
273+
thrownewCacheException(sprintf('MemcachedAdapter: "prefix_key" option must be empty when using proxified connections, "%s" given.',$prefix));
274+
}
275+
276+
return$this->client =$this->lazyClient;
277+
}
253278
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp