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: handle private separately #58

Merged
merged 1 commit into from
Dec 31, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cache/CachePublic.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
public function tokenize(Tokenizer $tokenizer): Tokenizer
{
if (!$tokenizer->public) {
if ($tokenizer->private) {
return $tokenizer;
}

Expand Down
12 changes: 10 additions & 2 deletions src/Cache/Control/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public function __construct(
/**
* @var bool indicates that any caches of any kind (public or shared) should not store this response.
*/
public bool $public = true,
public bool $public = false,

/**
* @var bool indicates that a public cache should not cache this
*/
public bool $private = false,

/**
* @var bool indicates that the response will not be updated while it's fresh.
Expand Down Expand Up @@ -68,6 +73,7 @@ public function with(
bool|null $proxyRevalidate = null,
bool|null $noStore = null,
bool|null $public = null,
bool|null $private = null,
bool|null $immutable = null,
int|null|false $staleWhileRevalidating = false,
int|null|false $staleIfError = false,
Expand All @@ -80,6 +86,7 @@ public function with(
proxyRevalidate: $this->withBool($this->proxyRevalidate, $proxyRevalidate),
noStore: $this->withBool($this->noStore, $noStore),
public: $this->withBool($this->public, $public),
private: $this->withBool($this->private, $private),
immutable: $this->withBool($this->immutable, $immutable),
staleWhileRevalidating: $this->withInt($this->staleWhileRevalidating, $staleWhileRevalidating),
staleIfError: $this->withInt($this->staleIfError, $staleIfError),
Expand All @@ -99,7 +106,8 @@ private function withBool(bool $original, bool|null $var): bool
public function render(): string
{
$header = [
$this->public ? 'public' : 'private',
...$this->header($this->private, 'private'),
...$this->header($this->public, 'public'),
...$this->header($this->maxAge, "max-age=$this->maxAge"),
...$this->header($this->sMaxAge, "s-maxage=$this->sMaxAge"),
...$this->header($this->noCache, "no-cache"),
Expand Down
3 changes: 2 additions & 1 deletion src/Cache/NeverCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public function tokenize(Tokenizer $tokenizer): Tokenizer
proxyRevalidate: false,
noStore: true,
public: false,
private: true,
immutable: false,
staleWhileRevalidating: null,
staleIfError: null
staleIfError: null,
);
}
}
2 changes: 1 addition & 1 deletion src/Cache/UserSpecific.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
{
public function tokenize(Tokenizer $tokenizer): Tokenizer
{
return $tokenizer->with(public: false);
return $tokenizer->with(public: false, private: true);
}
}
11 changes: 10 additions & 1 deletion tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string

it('can describe a simple public cache', function () {
$tokenizer = new Tokenizer();
expect(render($tokenizer))->toBe('public');
$directives[] = new CachePublic();
expect(render($tokenizer, ...$directives))->toBe('public');
});

it('can handle max age rules', function () {
$tokenizer = new Tokenizer();

$directives[] = new NeverChanges();
$directives[] = new CachePublic();
expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable");

$directives[] = new MaxAge(3000);
Expand All @@ -54,6 +56,7 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string
$tokenizer = new Tokenizer();

$directives[] = new NeverChanges();
$directives[] = new CachePublic();
expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable");
$directives[] = new Revalidate(RevalidationEnum::AfterError, 300);
expect(render($tokenizer, ...$directives))->toBe("public stale-if-error=300");
Expand Down Expand Up @@ -96,3 +99,9 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string
$directives[] = new CachePublic();
expect(render($tokenizer, ...$directives))->toBe("private");
});

it('renders nothing when empty', function() {
$tokenizer = new Tokenizer();

expect(render($tokenizer))->toBe('');
});
Loading