Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] Propagate expiry when syncing items in ChainAdapter #34787

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have preferred to keep reading from $sourceItem->metadata here, but this is cleared by

unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]);
when the adapter is a CacheInterface so have provided the metadata separately to work around this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cleared only if the expiry is over, so not sure about this argument

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't appear to be the case in my testing. The condition makes sense when setting metadata based on the item properties set by the cache interface build callback, however in the case of the chain adapter the wrapper callback will be returning an item fetched from the cache which does not have the expiry property set (see #34487). Thus the metadata is cleared.


$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