Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77138ac

Browse files
Trevor Northnicolas-grekas
Trevor North
authored andcommitted
[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.
1 parent 5da657b commit 77138ac

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public function __construct(array $adapters, int $defaultLifetime = 0)
6161
$this->adapterCount = \count($this->adapters);
6262

6363
$this->syncItem = \Closure::bind(
64-
static function ($sourceItem, $item) use ($defaultLifetime) {
64+
static function ($sourceItem, $item, $sourceMetadata = null) use ($defaultLifetime) {
65+
$sourceItem->isTaggable = false;
66+
$sourceMetadata = $sourceMetadata ?? $sourceItem->metadata;
67+
unset($sourceMetadata[CacheItem::METADATA_TAGS]);
68+
6569
$item->value = $sourceItem->value;
66-
$item->expiry = $sourceItem->expiry;
70+
$item->expiry = $sourceMetadata[CacheItem::METADATA_EXPIRY] ?? $sourceItem->expiry;
6771
$item->isHit = $sourceItem->isHit;
68-
$item->metadata = $sourceItem->metadata;
69-
70-
$sourceItem->isTaggable = false;
71-
unset($sourceItem->metadata[CacheItem::METADATA_TAGS]);
72+
$item->metadata = $item->newMetadata = $sourceItem->metadata = $sourceMetadata;
7273

7374
if (0 < $sourceItem->defaultLifetime && $sourceItem->defaultLifetime < $defaultLifetime) {
7475
$defaultLifetime = $sourceItem->defaultLifetime;
@@ -103,7 +104,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
103104
$value = $this->doGet($adapter, $key, $callback, $beta, $metadata);
104105
}
105106
if (null !== $item) {
106-
($this->syncItem)($lastItem = $lastItem ?? $item, $item);
107+
($this->syncItem)($lastItem = $lastItem ?? $item, $item, $metadata);
107108
}
108109

109110
return $value;

src/Symfony/Component/Cache/Tests/Adapter/ChainAdapterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ChainAdapterTest extends AdapterTestCase
2828
public function createCachePool($defaultLifetime = 0, $testMethod = null)
2929
{
3030
if ('testGetMetadata' === $testMethod) {
31-
return new ChainAdapter([new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
31+
return new ChainAdapter([new FilesystemAdapter('a', $defaultLifetime), new FilesystemAdapter('b', $defaultLifetime)], $defaultLifetime);
3232
}
3333

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

0 commit comments

Comments
 (0)