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

Skip to content

Commit 467f0ae

Browse files
[Cache] serialize objects using native arrays when possible
1 parent 1b2bd8f commit 467f0ae

File tree

8 files changed

+378
-78
lines changed

8 files changed

+378
-78
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,21 @@ public function get(string $key, callable $callback)
8888
if (null === $this->values) {
8989
$this->initialize();
9090
}
91-
if (null === $value = $this->values[$key] ?? null) {
91+
if (null === $value = $this->keys[$key] ?? null) {
9292
if ($this->pool instanceof CacheInterface) {
9393
return $this->pool->get($key, $callback);
9494
}
9595

9696
return $this->doGet($this->pool, $key, $callback);
9797
}
98+
$value = $this->values[$value];
99+
98100
if ('N;' === $value) {
99101
return null;
100102
}
103+
if ($value instanceof \Closure) {
104+
return $value();
105+
}
101106
if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
102107
return unserialize($value);
103108
}
@@ -116,15 +121,17 @@ public function getItem($key)
116121
if (null === $this->values) {
117122
$this->initialize();
118123
}
119-
if (!isset($this->values[$key])) {
124+
if (null === $value = $this->keys[$key] ?? null) {
120125
return $this->pool->getItem($key);
121126
}
122127

123-
$value = $this->values[$key];
128+
$value = $this->values[$value];
124129
$isHit = true;
125130

126131
if ('N;' === $value) {
127132
$value = null;
133+
} elseif ($value instanceof \Closure) {
134+
$value = $value();
128135
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
129136
try {
130137
$value = unserialize($value);
@@ -168,7 +175,7 @@ public function hasItem($key)
168175
$this->initialize();
169176
}
170177

171-
return isset($this->values[$key]) || $this->pool->hasItem($key);
178+
return isset($this->keys[$key]) || $this->pool->hasItem($key);
172179
}
173180

174181
/**
@@ -183,7 +190,7 @@ public function deleteItem($key)
183190
$this->initialize();
184191
}
185192

186-
return !isset($this->values[$key]) && $this->pool->deleteItem($key);
193+
return !isset($this->keys[$key]) && $this->pool->deleteItem($key);
187194
}
188195

189196
/**
@@ -199,7 +206,7 @@ public function deleteItems(array $keys)
199206
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
200207
}
201208

202-
if (isset($this->values[$key])) {
209+
if (isset($this->keys[$key])) {
203210
$deleted = false;
204211
} else {
205212
$fallbackKeys[] = $key;
@@ -225,7 +232,7 @@ public function save(CacheItemInterface $item)
225232
$this->initialize();
226233
}
227234

228-
return !isset($this->values[$item->getKey()]) && $this->pool->save($item);
235+
return !isset($this->keys[$item->getKey()]) && $this->pool->save($item);
229236
}
230237

231238
/**
@@ -237,7 +244,7 @@ public function saveDeferred(CacheItemInterface $item)
237244
$this->initialize();
238245
}
239246

240-
return !isset($this->values[$item->getKey()]) && $this->pool->saveDeferred($item);
247+
return !isset($this->keys[$item->getKey()]) && $this->pool->saveDeferred($item);
241248
}
242249

243250
/**
@@ -254,11 +261,13 @@ private function generateItems(array $keys): \Generator
254261
$fallbackKeys = array();
255262

256263
foreach ($keys as $key) {
257-
if (isset($this->values[$key])) {
258-
$value = $this->values[$key];
264+
if (isset($this->keys[$key])) {
265+
$value = $this->values[$this->keys[$key]];
259266

260267
if ('N;' === $value) {
261268
yield $key => $f($key, null, true);
269+
} elseif ($value instanceof \Closure) {
270+
yield $key => $f($key, $value(), true);
262271
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
263272
try {
264273
yield $key => $f($key, unserialize($value), true);

src/Symfony/Component/Cache/Simple/PhpArrayCache.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ public function get($key, $default = null)
6767
if (null === $this->values) {
6868
$this->initialize();
6969
}
70-
if (!isset($this->values[$key])) {
70+
if (null === $value = $this->keys[$key] ?? null) {
7171
return $this->pool->get($key, $default);
7272
}
73-
74-
$value = $this->values[$key];
73+
$value = $this->values[$value];
7574

7675
if ('N;' === $value) {
7776
return null;
7877
}
78+
if ($value instanceof \Closure) {
79+
return $value();
80+
}
7981
if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
8082
try {
8183
return unserialize($value);
@@ -121,7 +123,7 @@ public function has($key)
121123
$this->initialize();
122124
}
123125

124-
return isset($this->values[$key]) || $this->pool->has($key);
126+
return isset($this->keys[$key]) || $this->pool->has($key);
125127
}
126128

127129
/**
@@ -136,7 +138,7 @@ public function delete($key)
136138
$this->initialize();
137139
}
138140

139-
return !isset($this->values[$key]) && $this->pool->delete($key);
141+
return !isset($this->keys[$key]) && $this->pool->delete($key);
140142
}
141143

142144
/**
@@ -156,7 +158,7 @@ public function deleteMultiple($keys)
156158
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
157159
}
158160

159-
if (isset($this->values[$key])) {
161+
if (isset($this->keys[$key])) {
160162
$deleted = false;
161163
} else {
162164
$fallbackKeys[] = $key;
@@ -185,7 +187,7 @@ public function set($key, $value, $ttl = null)
185187
$this->initialize();
186188
}
187189

188-
return !isset($this->values[$key]) && $this->pool->set($key, $value, $ttl);
190+
return !isset($this->keys[$key]) && $this->pool->set($key, $value, $ttl);
189191
}
190192

191193
/**
@@ -205,7 +207,7 @@ public function setMultiple($values, $ttl = null)
205207
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
206208
}
207209

208-
if (isset($this->values[$key])) {
210+
if (isset($this->keys[$key])) {
209211
$saved = false;
210212
} else {
211213
$fallbackValues[$key] = $value;
@@ -224,11 +226,13 @@ private function generateItems(array $keys, $default)
224226
$fallbackKeys = array();
225227

226228
foreach ($keys as $key) {
227-
if (isset($this->values[$key])) {
228-
$value = $this->values[$key];
229+
if (isset($this->keys[$key])) {
230+
$value = $this->values[$this->keys[$key]];
229231

230232
if ('N;' === $value) {
231233
yield $key => null;
234+
} elseif ($value instanceof \Closure) {
235+
yield $key => $value();
232236
} elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
233237
try {
234238
yield $key => unserialize($value);

src/Symfony/Component/Cache/Tests/Adapter/PhpArrayAdapterTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,32 @@ public function testStore()
102102

103103
public function testStoredFile()
104104
{
105-
$expected = array(
105+
$data = array(
106106
'integer' => 42,
107107
'float' => 42.42,
108108
'boolean' => true,
109109
'array_simple' => array('foo', 'bar'),
110110
'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
111111
);
112+
$expected = array(
113+
array(
114+
'integer' => 0,
115+
'float' => 1,
116+
'boolean' => 2,
117+
'array_simple' => 3,
118+
'array_associative' => 4,
119+
),
120+
array(
121+
0 => 42,
122+
1 => 42.42,
123+
2 => true,
124+
3 => array('foo', 'bar'),
125+
4 => array('foo' => 'bar', 'foo2' => 'bar2'),
126+
),
127+
);
112128

113129
$adapter = $this->createCachePool();
114-
$adapter->warmUp($expected);
130+
$adapter->warmUp($data);
115131

116132
$values = eval(substr(file_get_contents(self::$file), 6));
117133

@@ -121,12 +137,16 @@ public function testStoredFile()
121137

122138
class PhpArrayAdapterWrapper extends PhpArrayAdapter
123139
{
140+
protected $data = array();
141+
124142
public function save(CacheItemInterface $item)
125143
{
126144
call_user_func(\Closure::bind(function () use ($item) {
127-
$this->values[$item->getKey()] = $item->get();
128-
$this->warmUp($this->values);
129-
$this->values = eval(substr(file_get_contents($this->file), 6));
145+
$key = $item->getKey();
146+
$this->keys[$key] = $id = \count($this->values);
147+
$this->data[$key] = $this->values[$id] = $item->get();
148+
$this->warmUp($this->data);
149+
list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
130150
}, $this, PhpArrayAdapter::class));
131151

132152
return true;

src/Symfony/Component/Cache/Tests/Simple/PhpArrayCacheTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,32 @@ public function testStore()
9595

9696
public function testStoredFile()
9797
{
98-
$expected = array(
98+
$data = array(
9999
'integer' => 42,
100100
'float' => 42.42,
101101
'boolean' => true,
102102
'array_simple' => array('foo', 'bar'),
103103
'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
104104
);
105+
$expected = array(
106+
array(
107+
'integer' => 0,
108+
'float' => 1,
109+
'boolean' => 2,
110+
'array_simple' => 3,
111+
'array_associative' => 4,
112+
),
113+
array(
114+
0 => 42,
115+
1 => 42.42,
116+
2 => true,
117+
3 => array('foo', 'bar'),
118+
4 => array('foo' => 'bar', 'foo2' => 'bar2'),
119+
),
120+
);
105121

106122
$cache = new PhpArrayCache(self::$file, new NullCache());
107-
$cache->warmUp($expected);
123+
$cache->warmUp($data);
108124

109125
$values = eval(substr(file_get_contents(self::$file), 6));
110126

@@ -114,12 +130,14 @@ public function testStoredFile()
114130

115131
class PhpArrayCacheWrapper extends PhpArrayCache
116132
{
133+
protected $data = array();
134+
117135
public function set($key, $value, $ttl = null)
118136
{
119137
call_user_func(\Closure::bind(function () use ($key, $value) {
120-
$this->values[$key] = $value;
121-
$this->warmUp($this->values);
122-
$this->values = eval(substr(file_get_contents($this->file), 6));
138+
$this->data[$key] = $value;
139+
$this->warmUp($this->data);
140+
list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
123141
}, $this, PhpArrayCache::class));
124142

125143
return true;
@@ -132,10 +150,10 @@ public function setMultiple($values, $ttl = null)
132150
}
133151
call_user_func(\Closure::bind(function () use ($values) {
134152
foreach ($values as $key => $value) {
135-
$this->values[$key] = $value;
153+
$this->data[$key] = $value;
136154
}
137-
$this->warmUp($this->values);
138-
$this->values = eval(substr(file_get_contents($this->file), 6));
155+
$this->warmUp($this->data);
156+
list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
139157
}, $this, PhpArrayCache::class));
140158

141159
return true;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ private function init($namespace, $defaultLifetime, $version)
5252
protected function doFetch(array $ids)
5353
{
5454
try {
55+
$values = array();
5556
foreach (apcu_fetch($ids, $ok) ?: array() as $k => $v) {
5657
if (null !== $v || $ok) {
57-
yield $k => $v;
58+
$values[$k] = $v;
5859
}
5960
}
61+
62+
return $values;
6063
} catch (\Error $e) {
6164
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
6265
}

0 commit comments

Comments
 (0)