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

Appearance settings

Commit 1a7398b

Browse filesBrowse files
[Cache] Don't clone, serialize
1 parent 0bacaba commit 1a7398b
Copy full SHA for 1a7398b

File tree

3 files changed

+26
-23
lines changed
Filter options

3 files changed

+26
-23
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
+26-9Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ 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+
public function __construct($defaultLifetime = 0, $storeSerialized = true)
3334
{
35+
$this->storeSerialized = $storeSerialized;
3436
$this->createCacheItem = \Closure::bind(
3537
function ($key, $value, $isHit) use ($defaultLifetime) {
3638
$item = new CacheItem();
@@ -52,9 +54,17 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
5254
public function getItem($key)
5355
{
5456
$f = $this->createCacheItem;
55-
$isHit = $this->hasItem($key);
57+
if ($isHit = $this->hasItem($key)) {
58+
$value = $this->values[$key];
5659

57-
return $f($key, $isHit ? $this->values[$key] : null, $isHit);
60+
if ($this->storeSerialized) {
61+
$value = unserialize($value);
62+
}
63+
} else {
64+
$value = null;
65+
}
66+
67+
return $f($key, $value, $isHit);
5868
}
5969

6070
/**
@@ -125,13 +135,12 @@ public function save(CacheItemInterface $item)
125135
if (0 > $lifetime) {
126136
return true;
127137
}
128-
129-
if (is_object($value)) {
138+
if ($this->storeSerialized) {
130139
try {
131-
$value = clone $value;
140+
$value = serialize($value);
132141
} catch (\Exception $e) {
133142
$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));
143+
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
135144

136145
return false;
137146
}
@@ -179,9 +188,17 @@ private function generateItems(array $keys)
179188
$f = $this->createCacheItem;
180189

181190
foreach ($keys as $key) {
182-
$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
191+
if ($isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key))) {
192+
$value = $this->values[$key];
193+
194+
if ($this->storeSerialized) {
195+
$value = unserialize($value);
196+
}
197+
} else {
198+
$value = null;
199+
}
183200

184-
yield $key => $f($key, $isHit ? $this->values[$key] : null, $isHit);
201+
yield $key => $f($key, $value, $isHit);
185202
}
186203
}
187204
}

‎src/Symfony/Component/Cache/CacheItem.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/CacheItem.php
-7Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.