Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,9 @@ public function stopCollectingCacheInfo(): array
if (!empty($this->_cacheTagBuffers)) {
$this->_cacheTags = array_merge(array_pop($this->_cacheTagBuffers), $tags);

// Override the parent duration if ours is shorter
// Preserve the shortest duration across nested collections
$this->_cacheDuration = array_pop($this->_cacheDurationBuffers);
if ($duration && $duration < $this->_cacheDuration) {
if ($duration && (!$this->_cacheDuration || $duration < $this->_cacheDuration)) {
$this->_cacheDuration = $duration;
}
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/services/ElementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Craft;
use craft\elements\Entry;
use craft\helpers\DateTimeHelper;
use craft\services\Elements;
use craft\test\TestCase;
use craft\test\TestSetup;
Expand Down Expand Up @@ -83,6 +84,20 @@ public function testParseRefs(): void
}
}

public function testNestedCacheInfoPreservesExpiry(): void
{
$this->elements->startCollectingCacheInfo();
$this->elements->collectCacheTags(['test']);
$this->elements->startCollectingCacheInfo();
$this->elements->setCacheExpiryDate(DateTimeHelper::now()->modify('+60 seconds'));
$this->elements->stopCollectingCacheInfo();
[, $duration] = $this->elements->stopCollectingCacheInfo();

self::assertNotNull($duration);
self::assertGreaterThan(0, $duration);
self::assertLessThanOrEqual(60, $duration);
}

/**
* @inheritdoc
*/
Expand Down
Loading