Skip to content

Commit

Permalink
Improve tag logging
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Oct 18, 2024
1 parent ccf2a37 commit 0137bfb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
31 changes: 16 additions & 15 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,20 @@ private function addCacheHeadersToWebResponse(): void
"public, s-maxage=$this->cacheDuration, max-age=0",
);

// Capture, remove any existing headers so we can prepare them
// Capture and remove any existing headers so we can prepare them
$existingTagsFromHeader = Collection::make($headers->get(HeaderEnum::CACHE_TAG->value, first: false) ?? []);
$headers->remove(HeaderEnum::CACHE_TAG->value);
$this->tags = $this->tags->push(...$existingTagsFromHeader);
$this->tags->push(...$existingTagsFromHeader);
$this->tags = $this->prepareTags(...$this->tags);

$this->prepareTags(...$this->tags)
->each(fn(string $tag) => $headers->add(
Craft::warning(new PsrMessage('Adding cache tags to response', [
'tags' => $this->tags,
]));

$this->tags
->each(fn(StaticCacheTag $tag) => $headers->add(
HeaderEnum::CACHE_TAG->value,
$tag,
$tag->getValue(),
));
}

Expand All @@ -257,13 +262,13 @@ public function purgeTags(string|StaticCacheTag ...$tags): void
}

Craft::info(new PsrMessage('Purging tags', [
'tags' => $tags->all(),
'tags' => $tags,
]));

if ($isWebResponse) {
$tags->each(fn(string $tag) => $response->getHeaders()->add(
$tags->each(fn(StaticCacheTag $tag) => $response->getHeaders()->add(
HeaderEnum::CACHE_PURGE_TAG->value,
$tag,
$tag->getValue(),
));

return;
Expand Down Expand Up @@ -306,12 +311,8 @@ private function isCacheable(): bool
private function prepareTags(string|StaticCacheTag ...$tags): Collection
{
return Collection::make($tags)
->map(function(string|StaticCacheTag $tag): string {
$tag = is_string($tag) ? StaticCacheTag::create($tag) : $tag;

return $tag->getValue();
})
->filter()
->unique();
->map(fn(string|StaticCacheTag $tag) => is_string($tag) ? StaticCacheTag::create($tag) : $tag)
->filter(fn(StaticCacheTag $tag) => (bool) $tag->getValue())
->unique(fn(StaticCacheTag $tag) => $tag->getValue());
}
}
19 changes: 14 additions & 5 deletions src/StaticCacheTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace craft\cloud;

class StaticCacheTag implements \Stringable
class StaticCacheTag implements \Stringable, \JsonSerializable
{
public readonly string $originalValue;
private bool $minify = true;
Expand All @@ -18,23 +18,32 @@ public static function create(string $value): self
return new self($value);
}

public function jsonSerialize(): false|string
{
return json_encode([
'value' => $this->getValue(),
'originalValue' => $this->originalValue,
]);
}

public function __toString(): string
{
return $this->getValue();
}

public function getValue(): string
{
$this->removeInvalidCharacters();
$clone = clone $this;
$clone->removeInvalidCharacters();

if ($this->minify) {
return $this
if ($clone->value && $clone->minify) {
return self::create($clone->value)
->hash()
->withPrefix(Module::getInstance()->getConfig()->getShortEnvironmentId())
->value;
}

return $this->value;
return $clone->value;
}

public function withPrefix(string $prefix): self
Expand Down

0 comments on commit 0137bfb

Please sign in to comment.