Skip to content

Commit

Permalink
Merge branch '1.x' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed May 10, 2024
2 parents c5b2ff5 + abd7e16 commit a31a580
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class StaticCache extends \yii\base\Component
private ?int $cacheDuration = null;
private Collection $tags;
private Collection $tagsToPurge;
private bool $collectingCacheInfo = false;

public function init(): void
{
Expand Down Expand Up @@ -102,6 +103,7 @@ public function handleInitWebApplication(Event $event): void
}

Craft::$app->getElements()->startCollectingCacheInfo();
$this->collectingCacheInfo = true;
}

public function handleAfterPrepareWebResponse(Event $event): void
Expand All @@ -110,14 +112,15 @@ public function handleAfterPrepareWebResponse(Event $event): void
return;
}

/** @var TagDependency|null $dependency */
/** @var int|null $duration */
[$dependency, $duration] = Craft::$app->getElements()->stopCollectingCacheInfo();
$tags = $dependency?->tags ?? [];
$this->tags->push(...$tags);

// Don't override the cache duration if it's already set
$this->cacheDuration = $this->cacheDuration ?? $duration;
if ($this->collectingCacheInfo) {
/** @var TagDependency|null $dependency */
/** @var int|null $duration */
[$dependency, $duration] = Craft::$app->getElements()->stopCollectingCacheInfo();
$this->collectingCacheInfo = false;
$tags = $dependency?->tags ?? [];
$this->tags->push(...$tags);
$this->cacheDuration = $duration;
}

$this->addCacheHeadersToWebResponse();
}
Expand Down Expand Up @@ -217,7 +220,6 @@ private function addCacheHeadersToWebResponse(): void
$headers->remove(HeaderEnum::CACHE_TAG->value);
$this->tags = $this->tags->push(...$existingTagsFromHeader);

// TODO: should I reassign this back to $this->tags with prepared values?
// Header value can't exceed 16KB
// https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#a-few-things-to-remember
$this->prepareTags(...$this->tags)
Expand Down

0 comments on commit a31a580

Please sign in to comment.