From 77138acea6ffecab2fa67e409c9102cf489ac7de Mon Sep 17 00:00:00 2001 From: Trevor North Date: Tue, 3 Dec 2019 17:14:15 +0000 Subject: [PATCH] [Cache] Propagate expiry when syncing items in ChainAdapter If a lower adapter provides item metadata, propagate the expiry time when syncing the item to upper ones. --- .../Component/Cache/Adapter/ChainAdapter.php | 17 +++++++++-------- .../Cache/Tests/Adapter/ChainAdapterTest.php | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php b/src/Symfony/Component/Cache/Adapter/ChainAdapter.php index 0217c801e4..aad2bc1bd8 100644 --- a/src/Symfony/Component/Cache/Adapter/ChainAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ChainAdapter.php @@ -61,14 +61,15 @@ class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterfa $this->adapterCount = \count($this->adapters); $this->syncItem = \Closure::bind( - static function ($sourceItem, $item) use ($defaultLifetime) { - $item->value = $sourceItem->value; - $item->expiry = $sourceItem->expiry; - $item->isHit = $sourceItem->isHit; - $item->metadata = $sourceItem->metadata; - + static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifetime) { $sourceItem->isTaggable = false; - unset($sourceItem->metadata[CacheItem::METADATA_TAGS]); + $sourceMetadata = $sourceMetadata ?? $sourceItem->metadata; + unset($sourceMetadata[CacheItem::METADATA_TAGS]); + + $item->value = $sourceItem->value; + $item->expiry = $sourceMetadata[CacheItem::METADATA_EXPIRY] ?? $sourceItem->expiry; + $item->isHit = $sourceItem->isHit; + $item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata; if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) { $defaultLifetime = $sourceItem->defaultLifetime; @@ -103,7 +104,7 @@ class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterfa $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; diff --git a/src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php index ac92006404..74f53804ef 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php @@ -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);