Skip to content

Commit df3cc55

Browse files
[Cache] Handle serialization failures for Memcached
1 parent 5556a3a commit df3cc55

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ trait MemcachedTrait
2626
'persistent_id' => null,
2727
'username' => null,
2828
'password' => null,
29+
'serializer' => 'php',
2930
);
3031

3132
private $client;
@@ -186,15 +187,32 @@ public static function createConnection($servers, array $options = array())
186187
*/
187188
protected function doSave(array $values, $lifetime)
188189
{
189-
return $this->checkResultCode($this->client->setMulti($values, $lifetime));
190+
$serialized = array();
191+
$failed = array();
192+
193+
foreach ($values as $id => $value) {
194+
try {
195+
$serialized[$id] = serialize($value);
196+
} catch (\Exception $e) {
197+
$failed[] = $id;
198+
}
199+
}
200+
201+
if (!$serialized) {
202+
return $failed;
203+
}
204+
205+
return $this->checkResultCode($this->client->setMulti($serialized, $lifetime)) ? $failed : false;
190206
}
191207

192208
/**
193209
* {@inheritdoc}
194210
*/
195211
protected function doFetch(array $ids)
196212
{
197-
return $this->checkResultCode($this->client->getMulti($ids));
213+
foreach ($this->checkResultCode($this->client->getMulti($ids)) as $id => $value) {
214+
yield $id => parent::unserialize($value);
215+
}
198216
}
199217

200218
/**

0 commit comments

Comments
 (0)