Skip to content

Commit 98b9be9

Browse files
committed
[Cache] fix using ProxyAdapter inside TagAwareAdapter
1 parent c8d6dec commit 98b9be9

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defa
4545
function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
4646
$item = new CacheItem();
4747
$item->key = $key;
48-
$item->value = $innerItem->get();
49-
$item->isHit = $innerItem->isHit();
5048
$item->defaultLifetime = $defaultLifetime;
51-
$item->innerItem = $innerItem;
5249
$item->poolHash = $poolHash;
53-
$innerItem->set(null);
50+
51+
if (null !== $innerItem) {
52+
$item->value = $innerItem->get();
53+
$item->isHit = $innerItem->isHit();
54+
$item->innerItem = $innerItem;
55+
$innerItem->set(null);
56+
}
5457

5558
return $item;
5659
},
@@ -156,7 +159,18 @@ private function doSave(CacheItemInterface $item, $method)
156159
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
157160
$expiry = time() + $item["\0*\0defaultLifetime"];
158161
}
159-
$innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
162+
163+
if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
164+
$innerItem = $item["\0*\0innerItem"];
165+
} elseif ($this->pool instanceof AdapterInterface) {
166+
// this is an optimization specific for AdapterInterface implementations
167+
// so we can save a round-trip to the backend by just creating a new item
168+
$f = $this->createCacheItem;
169+
$innerItem = $f($this->namespace.$item["\0*\0key"], null);
170+
} else {
171+
$innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
172+
}
173+
160174
$innerItem->set($item["\0*\0value"]);
161175
$innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
162176

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function ($key, $value, CacheItem $protoItem) {
4848
$item->value = $value;
4949
$item->defaultLifetime = $protoItem->defaultLifetime;
5050
$item->expiry = $protoItem->expiry;
51-
$item->innerItem = $protoItem->innerItem;
5251
$item->poolHash = $protoItem->poolHash;
5352

5453
return $item;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Symfony\Component\Cache\Tests\Adapter;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Psr\Cache\CacheItemPoolInterface;
7+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
8+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
9+
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
10+
use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
11+
12+
class TagAwareAndProxyAdapterIntegrationTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider dataProvider
16+
*/
17+
public function testIntegrationUsingProxiedAdapter(CacheItemPoolInterface $proxiedAdapter)
18+
{
19+
$cache = new TagAwareAdapter(new ProxyAdapter($proxiedAdapter));
20+
21+
$item = $cache->getItem('foo');
22+
$item->tag(['tag1', 'tag2']);
23+
$item->set('bar');
24+
$cache->save($item);
25+
26+
$this->assertSame('bar', $cache->getItem('foo')->get());
27+
}
28+
29+
public function dataProvider()
30+
{
31+
return [
32+
[new ArrayAdapter()],
33+
// also testing with a non-AdapterInterface implementation
34+
// because the ProxyAdapter behaves slightly different for those
35+
[new ExternalAdapter()],
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)