Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f524aa3

Browse files
[Cache] Don't clone, serialize
1 parent 0bacaba commit f524aa3

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,6 @@ public function saveDeferred(CacheItemInterface $item)
276276
if (!$item instanceof CacheItem) {
277277
return false;
278278
}
279-
try {
280-
$item = clone $item;
281-
} catch (\Exception $e) {
282-
$value = $item->get();
283-
$type = is_object($value) ? get_class($value) : gettype($value);
284-
CacheItem::log($this->logger, 'Failed to clone key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
285-
}
286279
$this->deferred[$item->getKey()] = $item;
287280

288281
return true;

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ class ArrayAdapter implements CacheItemPoolInterface, LoggerAwareInterface
2525
{
2626
use LoggerAwareTrait;
2727

28+
private $storeSerialized;
2829
private $values = array();
2930
private $expiries = array();
3031
private $createCacheItem;
3132

32-
public function __construct($defaultLifetime = 0)
33+
/**
34+
* @param int $defaultLifetime
35+
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise.
36+
*/
37+
public function __construct($defaultLifetime = 0, $storeSerialized = true)
3338
{
39+
$this->storeSerialized = $storeSerialized;
3440
$this->createCacheItem = \Closure::bind(
3541
function ($key, $value, $isHit) use ($defaultLifetime) {
3642
$item = new CacheItem();
@@ -51,10 +57,18 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
5157
*/
5258
public function getItem($key)
5359
{
60+
if ($isHit = $this->hasItem($key)) {
61+
if ($this->storeSerialized) {
62+
$value = unserialize($this->values[$key]);
63+
} else {
64+
$value = $this->values[$key];
65+
}
66+
} else {
67+
$value = null;
68+
}
5469
$f = $this->createCacheItem;
55-
$isHit = $this->hasItem($key);
5670

57-
return $f($key, $isHit ? $this->values[$key] : null, $isHit);
71+
return $f($key, $value, $isHit);
5872
}
5973

6074
/**
@@ -125,13 +139,12 @@ public function save(CacheItemInterface $item)
125139
if (0 > $lifetime) {
126140
return true;
127141
}
128-
129-
if (is_object($value)) {
142+
if ($this->storeSerialized) {
130143
try {
131-
$value = clone $value;
144+
$value = serialize($value);
132145
} catch (\Exception $e) {
133146
$type = is_object($value) ? get_class($value) : gettype($value);
134-
CacheItem::log($this->logger, 'Failed to clone key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
147+
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
135148

136149
return false;
137150
}
@@ -179,9 +192,17 @@ private function generateItems(array $keys)
179192
$f = $this->createCacheItem;
180193

181194
foreach ($keys as $key) {
182-
$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
195+
if ($isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key))) {
196+
if ($this->storeSerialized) {
197+
$value = unserialize($this->values[$key]);
198+
} else {
199+
$value = $this->values[$key];
200+
}
201+
} else {
202+
$value = null;
203+
}
183204

184-
yield $key => $f($key, $isHit ? $this->values[$key] : null, $isHit);
205+
yield $key => $f($key, $value, $isHit);
185206
}
186207
}
187208
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ final class CacheItem implements CacheItemInterface
3131
private $lifetime;
3232
private $defaultLifetime;
3333

34-
public function __clone()
35-
{
36-
if (is_object($this->value)) {
37-
$this->value = clone $this->value;
38-
}
39-
}
40-
4134
/**
4235
* {@inheritdoc}
4336
*/

0 commit comments

Comments
 (0)