8000 [Cache] Commit items implicitly only when deferred keys are requested · symfony/symfony@5abd014 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5abd014

Browse files
committed
[Cache] Commit items implicitly only when deferred keys are requested
1 parent 199c714 commit 5abd014

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function invalidateTags(array $tags)
157157
*/
158158
public function hasItem($key)
159159
{
160-
if ($this->deferred) {
160+
if (isset($this->deferred[$key])) {
161161
$this->commit();
162162
}
163163
if (!$this->pool->hasItem($key)) {
@@ -200,9 +200,6 @@ public function getItem($key)
200200
*/
201201
public function getItems(array $keys = [])
202202
{
203-
if ($this->deferred) {
204-
$this->commit();
205-
}
206203
$tagKeys = [];
207204

208205
foreach ($keys as $key) {
@@ -212,6 +209,10 @@ public function getItems(array $keys = [])
212209
}
213210
}
214211

212+
if ($this->deferred && (count($tagKeys) !== count($keys) || array_intersect_key($this->deferred, array_flip($keys)))) {
213+
$this->commit();
214+
}
215+
215216
try {
216217
$items = $this->pool->getItems($tagKeys + $keys);
217218
} catch (InvalidArgumentException $e) {

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ public function deleteItems(array $keys)
208208
*/
209209
public function getItem($key)
210210
{
211-
if ($this->deferred) {
211+
$id = $this->getId($key);
212+
213+
if (isset($this->deferred[$key])) {
212214
$this->commit();
213215
}
214-
$id = $this->getId($key);
215216

216217
$isHit = false;
217218
$value = null;
@@ -234,21 +235,22 @@ public function getItem($key)
234235
*/
235236
public function getItems(array $keys = [])
236237
{
237-
if ($this->deferred) {
238-
$this->commit();
239-
}
240238
$ids = [];
241-
242239
foreach ($keys as $key) {
243-
$ids[] = $this->getId($key);
240+
$ids[$key] = $this->getId($key);
244241
}
242+
243+
if ($this->deferred && array_intersect_key($this->deferred, $ids)) {
244+
$this->commit();
245+
}
246+
245247
try {
246248
$items = $this->doFetch($ids);
247249
} catch (\Exception $e) {
248250
CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
249251
$items = [];
250252
}
251-
$ids = array_combine($ids, $keys);
253+
$ids = array_flip($ids);
252254

253255
return $this->generateItems($items, $ids);
254256
}

0 commit comments

Comments
 (0)
0