Skip to content

Commit aa2ff9d

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

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ public function invalidateTags(array $tags)
157157
*/
158158
public function hasItem($key)
159159
{
160-
if ($this->deferred) {
160+
CacheItem::validateKey($key));
161+
if (isset($this->deferred[$key])) {
161162
$this->commit();
162163
}
163164
if (!$this->pool->hasItem($key)) {
@@ -200,9 +201,6 @@ public function getItem($key)
200201
*/
201202
public function getItems(array $keys = [])
202203
{
203-
if ($this->deferred) {
204-
$this->commit();
205-
}
206204
$tagKeys = [];
207205

208206
foreach ($keys as $key) {
@@ -212,6 +210,10 @@ public function getItems(array $keys = [])
212210
}
213211
}
214212

213+
if ($this->deferred && (count($tagKeys) !== count($keys) || array_intersect_key($this->deferred, array_flip($keys)))) {
214+
$this->commit();
215+
}
216+
215217
try {
216218
$items = $this->pool->getItems($tagKeys + $keys);
217219
} 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)