bug #30918 [Cache] fix using ProxyAdapter inside TagAwareAdapter (dmaicher)
This PR was merged into the 3.4 branch.
Discussion
----------
[Cache] fix using ProxyAdapter inside TagAwareAdapter
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #30400
| License | MIT
| Doc PR | -
EUFOSSA
After some debugging this is my first attempt to fix this issue @nicolas-grekas 😊 Let's discuss it.
without the fix the test fails like this:
```
Testing Symfony\Component\Cache\Tests\Adapter\TagAwareAndProxyAdapterIntegrationTest
F 1 / 1 (100%)
Time: 28 ms, Memory: 4.00MB
There was 1 failure:
1) Symfony\Component\Cache\Tests\Adapter\TagAwareAndProxyAdapterIntegrationTest::testIntegration
Failed asserting that Array &0 (
'tag1' => 0
'tag2' => 0
) is identical to 'bar'.
/var/www/symfony/src/Symfony/Component/Cache/Tests/Adapter/TagAwareAndProxyAdapterIntegrationTest.php:26
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Commits
-------
98b9be9b6a
[Cache] fix using ProxyAdapter inside TagAwareAdapter
This commit is contained in:
commit
77320cb645
@ -45,12 +45,15 @@ class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableIn
|
|||||||
function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
|
function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
|
||||||
$item = new CacheItem();
|
$item = new CacheItem();
|
||||||
$item->key = $key;
|
$item->key = $key;
|
||||||
$item->value = $innerItem->get();
|
|
||||||
$item->isHit = $innerItem->isHit();
|
|
||||||
$item->defaultLifetime = $defaultLifetime;
|
$item->defaultLifetime = $defaultLifetime;
|
||||||
$item->innerItem = $innerItem;
|
|
||||||
$item->poolHash = $poolHash;
|
$item->poolHash = $poolHash;
|
||||||
$innerItem->set(null);
|
|
||||||
|
if (null !== $innerItem) {
|
||||||
|
$item->value = $innerItem->get();
|
||||||
|
$item->isHit = $innerItem->isHit();
|
||||||
|
$item->innerItem = $innerItem;
|
||||||
|
$innerItem->set(null);
|
||||||
|
}
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
},
|
},
|
||||||
@ -156,7 +159,18 @@ class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableIn
|
|||||||
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
|
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
|
||||||
$expiry = time() + $item["\0*\0defaultLifetime"];
|
$expiry = time() + $item["\0*\0defaultLifetime"];
|
||||||
}
|
}
|
||||||
$innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
|
|
||||||
|
if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
|
||||||
|
$innerItem = $item["\0*\0innerItem"];
|
||||||
|
} elseif ($this->pool instanceof AdapterInterface) {
|
||||||
|
// this is an optimization specific for AdapterInterface implementations
|
||||||
|
// so we can save a round-trip to the backend by just creating a new item
|
||||||
|
$f = $this->createCacheItem;
|
||||||
|
$innerItem = $f($this->namespace.$item["\0*\0key"], null);
|
||||||
|
} else {
|
||||||
|
$innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
|
||||||
|
}
|
||||||
|
|
||||||
$innerItem->set($item["\0*\0value"]);
|
$innerItem->set($item["\0*\0value"]);
|
||||||
$innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
|
$innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
|
||||||
|
|
||||||
|
@ -48,7 +48,6 @@ class TagAwareAdapter implements TagAwareAdapterInterface, PruneableInterface, R
|
|||||||
$item->value = $value;
|
$item->value = $value;
|
||||||
$item->defaultLifetime = $protoItem->defaultLifetime;
|
$item->defaultLifetime = $protoItem->defaultLifetime;
|
||||||
$item->expiry = $protoItem->expiry;
|
$item->expiry = $protoItem->expiry;
|
||||||
$item->innerItem = $protoItem->innerItem;
|
|
||||||
$item->poolHash = $protoItem->poolHash;
|
$item->poolHash = $protoItem->poolHash;
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\Cache\Tests\Adapter;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||||
|
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
||||||
|
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
|
||||||
|
use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
|
||||||
|
|
||||||
|
class TagAwareAndProxyAdapterIntegrationTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider dataProvider
|
||||||
|
*/
|
||||||
|
public function testIntegrationUsingProxiedAdapter(CacheItemPoolInterface $proxiedAdapter)
|
||||||
|
{
|
||||||
|
$cache = new TagAwareAdapter(new ProxyAdapter($proxiedAdapter));
|
||||||
|
|
||||||
|
$item = $cache->getItem('foo');
|
||||||
|
$item->tag(['tag1', 'tag2']);
|
||||||
|
$item->set('bar');
|
||||||
|
$cache->save($item);
|
||||||
|
|
||||||
|
$this->assertSame('bar', $cache->getItem('foo')->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[new ArrayAdapter()],
|
||||||
|
// also testing with a non-AdapterInterface implementation
|
||||||
|
// because the ProxyAdapter behaves slightly different for those
|
||||||
|
[new ExternalAdapter()],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user