Skip to content

Commit

Permalink
bug #21166 [Cache] Fix order of writes in ChainAdapter (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.1 branch.

Discussion
----------

[Cache] Fix order of writes in ChainAdapter

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This is of importance in race situations: writes should happen on less volatile backend first so that.

Commits
-------

df552af [Cache] Fix order of writes in ChainAdapter
  • Loading branch information
nicolas-grekas committed Jan 5, 2017
2 parents 2904201 + df552af commit e2b11b4
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
class ChainAdapter implements AdapterInterface
{
private $adapters = array();
private $adapterCount;
private $saveUp;

/**
Expand All @@ -50,6 +51,7 @@ public function __construct(array $adapters, $maxLifetime = 0)
$this->adapters[] = new ProxyAdapter($adapter);
}
}
$this->adapterCount = count($this->adapters);

$this->saveUp = \Closure::bind(
function ($adapter, $item) use ($maxLifetime) {
Expand Down Expand Up @@ -146,9 +148,10 @@ public function hasItem($key)
public function clear()
{
$cleared = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$cleared = $adapter->clear() && $cleared;
while ($i--) {
$cleared = $this->adapters[$i]->clear() && $cleared;
}

return $cleared;
Expand All @@ -160,9 +163,10 @@ public function clear()
public function deleteItem($key)
{
$deleted = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$deleted = $adapter->deleteItem($key) && $deleted;
while ($i--) {
$deleted = $this->adapters[$i]->deleteItem($key) && $deleted;
}

return $deleted;
Expand All @@ -174,9 +178,10 @@ public function deleteItem($key)
public function deleteItems(array $keys)
{
$deleted = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$deleted = $adapter->deleteItems($keys) && $deleted;
while ($i--) {
$deleted = $this->adapters[$i]->deleteItems($keys) && $deleted;
}

return $deleted;
Expand All @@ -188,9 +193,10 @@ public function deleteItems(array $keys)
public function save(CacheItemInterface $item)
{
$saved = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$saved = $adapter->save($item) && $saved;
while ($i--) {
$saved = $this->adapters[$i]->save($item) && $saved;
}

return $saved;
Expand All @@ -202,9 +208,10 @@ public function save(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item)
{
$saved = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$saved = $adapter->saveDeferred($item) && $saved;
while ($i--) {
$saved = $this->adapters[$i]->saveDeferred($item) && $saved;
}

return $saved;
Expand All @@ -216,9 +223,10 @@ public function saveDeferred(CacheItemInterface $item)
public function commit()
{
$committed = true;
$i = $this->adapterCount;

foreach ($this->adapters as $adapter) {
$committed = $adapter->commit() && $committed;
while ($i--) {
$committed = $this->adapters[$i]->commit() && $committed;
}

return $committed;
Expand Down

0 comments on commit e2b11b4

Please sign in to comment.