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

Skip to content

Commit 71ec614

Browse files
sbelyshkinnicolas-grekas
authored andcommitted
[Cache] Commit items implicitly only when deferred keys are requested
1 parent 7237c75 commit 71ec614

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ public function invalidateTags(array $tags)
158158
*/
159159
public function hasItem($key)
160160
{
161-
if ($this->deferred) {
161+
if (\is_string($key) && isset($this->deferred[$key])) {
162162
$this->commit();
163163
}
164+
164165
if (!$this->pool->hasItem($key)) {
165166
return false;
166167
}
@@ -201,18 +202,21 @@ public function getItem($key)
201202
*/
202203
public function getItems(array $keys = [])
203204
{
204-
if ($this->deferred) {
205-
$this->commit();
206-
}
207205
$tagKeys = [];
206+
$commit = false;
208207

209208
foreach ($keys as $key) {
210209
if ('' !== $key && \is_string($key)) {
211210
$key = static::TAGS_PREFIX.$key;
212211
$tagKeys[$key] = $key;
212+
$commit = $commit || isset($this->deferred[$key]);
213213
}
214214
}
215215

216+
if ($commit) {
217+
$this->commit();
218+
}
219+
216220
try {
217221
$items = $this->pool->getItems($tagKeys + $keys);
218222
} catch (InvalidArgumentException $e) {

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ trait AbstractAdapterTrait
3939
*/
4040
public function getItem($key)
4141
{
42-
if ($this->deferred) {
42+
$id = $this->getId($key);
43+
44+
if (isset($this->deferred[$key])) {
4345
$this->commit();
4446
}
45-
$id = $this->getId($key);
4647

4748
$f = $this->createCacheItem;
4849
$isHit = false;
@@ -66,14 +67,18 @@ public function getItem($key)
6667
*/
6768
public function getItems(array $keys = [])
6869
{
69-
if ($this->deferred) {
70-
$this->commit();
71-
}
7270
$ids = [];
71+
$commit = false;
7372

7473
foreach ($keys as $key) {
7574
$ids[] = $this->getId($key);
75+
$commit = $commit || isset($this->deferred[$key]);
7676
}
77+
78+
if ($commit) {
79+
$this->commit();
80+
}
81+
7782
try {
7883
$items = $this->doFetch($ids);
7984
} catch (\Exception $e) {

0 commit comments

Comments
 (0)
0