bug #29648 [Cache] fix Simple\Psr6Cache proxying of metadata (nicolas-grekas)

This PR was merged into the 4.2 branch.

Discussion
----------

[Cache] fix Simple\Psr6Cache proxying of metadata

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

Discovered while working on #29236.

Commits
-------

02edc9b049 [Cache] fix Simple\Psr6Cache proxying of metadata
This commit is contained in:
Nicolas Grekas 2018-12-19 10:10:33 +01:00
commit 43dfbe2df9
2 changed files with 28 additions and 4 deletions

View File

@ -29,6 +29,8 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
{
use ProxyTrait;
private const METADATA_EXPIRY_OFFSET = 1527506807;
private $createCacheItem;
private $cacheItemPrototype;
@ -58,7 +60,7 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
}
$this->createCacheItem = $createCacheItem;
return $createCacheItem($key, $value, $allowInt);
return $createCacheItem($key, null, $allowInt)->set($value);
};
}
@ -147,8 +149,29 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
}
$values = array();
if (!$this->pool instanceof AdapterInterface) {
foreach ($items as $key => $item) {
$values[$key] = $item->isHit() ? $item->get() : $default;
}
return $value;
}
foreach ($items as $key => $item) {
$values[$key] = $item->isHit() ? $item->get() : $default;
if (!$item->isHit()) {
$values[$key] = $default;
continue;
}
$values[$key] = $item->get();
if (!$metadata = $item->getMetadata()) {
continue;
}
unset($metadata[CacheItem::METADATA_TAGS]);
if ($metadata) {
$values[$key] = array("\x9D".pack('VN', (int) $metadata[CacheItem::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET, $metadata[CacheItem::METADATA_CTIME])."\x5F" => $values[$key]);
}
}
return $values;

View File

@ -11,8 +11,9 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;
use Symfony\Component\Cache\Simple\FilesystemCache;
use Symfony\Component\Cache\Simple\Psr6Cache;
/**
* @group time-sensitive
@ -25,6 +26,6 @@ class SimpleCacheAdapterTest extends AdapterTestCase
public function createCachePool($defaultLifetime = 0)
{
return new SimpleCacheAdapter(new FilesystemCache(), '', $defaultLifetime);
return new SimpleCacheAdapter(new Psr6Cache(new FilesystemAdapter()), '', $defaultLifetime);
}
}