Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] fix using ProxyAdapter inside TagAwareAdapter #30918

Merged
merged 1 commit into from Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Expand Up @@ -45,12 +45,15 @@ public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defa
function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
$item = new CacheItem();
$item->key = $key;
$item->value = $innerItem->get();
$item->isHit = $innerItem->isHit();
$item->defaultLifetime = $defaultLifetime;
$item->innerItem = $innerItem;
$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;
},
Expand Down Expand Up @@ -156,7 +159,18 @@ private function doSave(CacheItemInterface $item, $method)
if (null === $expiry && 0 < $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->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);

Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Expand Up @@ -48,7 +48,6 @@ function ($key, $value, CacheItem $protoItem) {
$item->value = $value;
$item->defaultLifetime = $protoItem->defaultLifetime;
$item->expiry = $protoItem->expiry;
$item->innerItem = $protoItem->innerItem;
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
$item->poolHash = $protoItem->poolHash;

return $item;
Expand Down
@@ -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()],
];
}
}