Skip to content

Commit

Permalink
Merge pull request #26 from tv2regionerne/feature/cache-headers
Browse files Browse the repository at this point in the history
Support specifying cache headers in config
  • Loading branch information
Sylvester Damgaard authored Aug 30, 2024
2 parents d119c65 + 349ca0d commit 7b124cd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
29 changes: 27 additions & 2 deletions config/statamic-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,36 @@
return [
'enabled' => env('STATAMIC_AUTOCACHE_ENABLED', true),

/**
* Set the threshold at which we just flush the cache rather than
* invalidating individual pages
*/
'flush_cache_limit' => env('STATAMIC_AUTOCACHE_FLUSH_LIMIT', 1000),

/**
* Determine what headers will be set on responses depending
* on whether they were pulled from the cache or not
*/
'headers' => [
'hit' => [
'x-statamic-cache' => 'hit',
//'cache.headers' => 'no-cache,private,max-age=1200;etag', // will pass to laravel's middleware
],

'miss' => [
'x-statamic-cache' => 'miss',
//'cache.headers' => 'no-cache,private,max-age=900;etag', // will pass to laravel's middleware
],

'not-available' => [
'x-statamic-cache' => 'not-available',
//'cache.headers' => 'no-cache,private,max-age=900;etag', // will pass to laravel's middleware
],
],

/**
* Check if a database entry exists for the page when resolved from cache.
* Ensure consistency between cache and database, but requires each request to do a db query
*/
'split_brain_check' => env('STATAMIC_AUTOCACHE_SPLITBRAIN_CHECK', false),

'flush_cache_limit' => env('STATAMIC_AUTOCACHE_FLUSH_LIMIT', 1000),
];
25 changes: 22 additions & 3 deletions src/Http/Middleware/AutoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Tv2regionerne\StatamicCache\Http\Middleware;

use Closure;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Http\Response;
use Statamic\Contracts\Assets\Asset;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Globals\Variables;
use Statamic\Facades\URL;
use Statamic\Support\Arr;
use Statamic\Tags;
use Statamic\Taxonomies\LocalizedTerm;
use Tv2regionerne\StatamicCache\Facades\Store;
Expand Down Expand Up @@ -35,7 +37,7 @@ public function handle($request, Closure $next)
Store::removeWatcher($key);

if (! is_callable([$response, 'wasStaticallyCached'])) {
$response->headers->add(['x-statamic-cache' => 'not-available']);
$response = $this->setHeadersFromConfig($response, 'not-available');

$this->removeStaticCacheIfNoDataIsStored();

Expand All @@ -44,7 +46,7 @@ public function handle($request, Closure $next)

try {
if ($response->wasStaticallyCached()) {
$response->headers->add(['x-statamic-cache' => 'hit']);
$response = $this->setHeadersFromConfig($response, 'hit');

$this->removeStaticCacheIfNoDataIsStored();

Expand All @@ -54,13 +56,30 @@ public function handle($request, Closure $next)

}

$response->headers->add(['x-statamic-cache' => 'miss']);
$response = $this->setHeadersFromConfig($response, 'miss');

Store::addKeyMappingData($key);

return $response;
}

private function setHeadersFromConfig($response, string $type)
{
if (! $headers = config('statamic-cache.headers.'.$type, false)) {
return $response;
}

if ($options = Arr::pull($headers, 'cache.headers')) {
$response = (new SetCacheHeaders)->handle(request(), fn ($next) => $response, $options);
}

if ($headers = Arr::except($headers, ['cache.headers'])) {
$response->headers->add($headers);
}

return $response;
}

private function isEnabled($request)
{
if (! config('statamic-cache.enabled', true)) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Cacher/CacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,32 @@

$this->assertCount(0, StaticCache::all());
});

it('sets headers from config', function () {
config()->set('statamic-cache.headers', [
'hit' => [
'x-statamic-cache' => 'hit',
'cache.headers' => 'public;max_age=1628000;etag', // will pass to laravel's middleware
],

'miss' => [
'x-statamic-cache' => 'miss',
'cache.headers' => 'public;max_age=2628000;etag', // will pass to laravel's middleware
],

'not-available' => [
'x-statamic-cache' => 'not-available',
'cache.headers' => 'public;max_age=3628000;etag', // will pass to laravel's middleware
],
]);

$response = $this->get('/');

$this->assertSame($response->headers->get('cache-control'), 'max-age=2628000, no-cache, public');
$this->assertSame($response->headers->get('x-statamic-cache'), 'miss');

$response = $this->get('/');

$this->assertSame($response->headers->get('cache-control'), 'max-age=1628000, public');
$this->assertSame($response->headers->get('x-statamic-cache'), 'hit');
});

0 comments on commit 7b124cd

Please sign in to comment.