Skip to content

Commit

Permalink
bug #29648 [Cache] fix Simple\Psr6Cache proxying of metadata (nicolas…
Browse files Browse the repository at this point in the history
…-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
-------

02edc9b [Cache] fix Simple\Psr6Cache proxying of metadata
  • Loading branch information
nicolas-grekas committed Dec 19, 2018
2 parents 64e2449 + 02edc9b commit 43dfbe2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
27 changes: 25 additions & 2 deletions src/Symfony/Component/Cache/Simple/Psr6Cache.php
Expand Up @@ -29,6 +29,8 @@ class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterfa
{
use ProxyTrait;

private const METADATA_EXPIRY_OFFSET = 1527506807;

private $createCacheItem;
private $cacheItemPrototype;

Expand Down Expand Up @@ -58,7 +60,7 @@ function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
}
$this->createCacheItem = $createCacheItem;

return $createCacheItem($key, $value, $allowInt);
return $createCacheItem($key, null, $allowInt)->set($value);
};
}

Expand Down Expand Up @@ -147,8 +149,29 @@ public function getMultiple($keys, $default = null)
}
$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;
Expand Down
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 43dfbe2

Please sign in to comment.