Skip to content

Commit

Permalink
bug #43302 [Cache] Commit items implicitly only when deferred keys ar…
Browse files Browse the repository at this point in the history
…e requested (Sergey Belyshkin)

This PR was merged into the 4.4 branch.

Discussion
----------

[Cache] Commit items implicitly only when deferred keys are requested

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

Because of implicit commits before getting any item, deferred saves are useless.

```
for($r=0; $r<10; ++$r) {
    $item = $adapter->getItem('Key'.$r); // implicit commit
    $item->set('Value'.$r);
    $adapter->saveDeferred($item);
}
$adapter->commit(); // nothing to commit :-//
```
Let's commit deferred changes only when requested item was deferred previously.

Commits
-------

2e8c446 [Cache] Commit items implicitly only when deferred keys are requested
  • Loading branch information
nicolas-grekas committed Oct 6, 2021
2 parents 37d26fb + 2e8c446 commit 1231de6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions src/Symfony/Component/Cache/Traits/AbstractAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 1231de6

Please sign in to comment.