From 2e8c446a9188c99aa54eea6ea47c69bd9139de58 Mon Sep 17 00:00:00 2001 From: Sergey Belyshkin Date: Thu, 30 Sep 2021 16:37:34 +0700 Subject: [PATCH] [Cache] Commit items implicitly only when deferred keys are requested --- .../Component/Cache/Adapter/TagAwareAdapter.php | 12 ++++++++---- .../Cache/Traits/AbstractAdapterTrait.php | 15 ++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php index 72e03f9275ef7..cd96d2969b9ac 100644 --- a/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php @@ -158,9 +158,10 @@ public function invalidateTags(array $tags) */ public function hasItem($key) { - if ($this->deferred) { + if (\is_string($key) && isset($this->deferred[$key])) { $this->commit(); } + if (!$this->pool->hasItem($key)) { return false; } @@ -201,18 +202,21 @@ public function getItem($key) */ public function getItems(array $keys = []) { - if ($this->deferred) { - $this->commit(); - } $tagKeys = []; + $commit = false; foreach ($keys as $key) { if ('' !== $key && \is_string($key)) { + $commit = $commit || isset($this->deferred[$key]); $key = static::TAGS_PREFIX.$key; $tagKeys[$key] = $key; } } + if ($commit) { + $this->commit(); + } + try { $items = $this->pool->getItems($tagKeys + $keys); } catch (InvalidArgumentException $e) { diff --git a/src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php b/src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php index 8ff715d2f54e4..388c1df303188 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php @@ -39,10 +39,11 @@ trait AbstractAdapterTrait */ public function getItem($key) { - if ($this->deferred) { + $id = $this->getId($key); + + if (isset($this->deferred[$key])) { $this->commit(); } - $id = $this->getId($key); $f = $this->createCacheItem; $isHit = false; @@ -66,14 +67,18 @@ public function getItem($key) */ public function getItems(array $keys = []) { - if ($this->deferred) { - $this->commit(); - } $ids = []; + $commit = false; foreach ($keys as $key) { $ids[] = $this->getId($key); + $commit = $commit || isset($this->deferred[$key]); } + + if ($commit) { + $this->commit(); + } + try { $items = $this->doFetch($ids); } catch (\Exception $e) {