Skip to content

Commit

Permalink
[Cache] Propagate expiry when syncing items in ChainAdapter
Browse files Browse the repository at this point in the history
If a lower adapter provides item metadata, propagate the expiry time when
syncing the item to upper ones.
  • Loading branch information
Trevor North authored and nicolas-grekas committed Dec 10, 2019
1 parent 5da657b commit 77138ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/Symfony/Component/Cache/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public function __construct(array $adapters, int $defaultLifetime = 0)
$this->adapterCount = \count($this->adapters);

$this->syncItem = \Closure::bind(
static function ($sourceItem, $item) use ($defaultLifetime) {
static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifetime) {
$sourceItem->isTaggable = false;
$sourceMetadata = $sourceMetadata ?? $sourceItem->metadata;
unset($sourceMetadata[CacheItem::METADATA_TAGS]);

$item->value = $sourceItem->value;
$item->expiry = $sourceItem->expiry;
$item->expiry = $sourceMetadata[CacheItem::METADATA_EXPIRY] ?? $sourceItem->expiry;
$item->isHit = $sourceItem->isHit;
$item->metadata = $sourceItem->metadata;

$sourceItem->isTaggable = false;
unset($sourceItem->metadata[CacheItem::METADATA_TAGS]);
$item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;

if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
$defaultLifetime = $sourceItem->defaultLifetime;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
$value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
}
if (null !== $item) {
($this->syncItem)($lastItem = $lastItem ?? $item, $item);
($this->syncItem)($lastItem = $lastItem ?? $item, $item, $metadata);
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ChainAdapterTest extends AdapterTestCase
public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
if ('testGetMetadata' === $testMethod) {
return new ChainAdapter([new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
return new ChainAdapter([new FilesystemAdapter('a', $defaultLifetime), new FilesystemAdapter('b', $defaultLifetime)], $defaultLifetime);
}

return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
Expand Down

0 comments on commit 77138ac

Please sign in to comment.