From aa44b992ebbd6fd0239b352afbd69fbafee1359a Mon Sep 17 00:00:00 2001 From: "M. Vugteveen" Date: Mon, 16 May 2022 14:11:08 +0200 Subject: [PATCH] Add option to output cache age header (#385) * Add option to output cache age header * fix config * extract method * update readme --- README.md | 21 +++++++++++++++++---- config/responsecache.php | 14 ++++++++++++++ src/Middlewares/CacheResponse.php | 14 ++++++++++++++ tests/IntegrationTest.php | 18 ++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 267ffc0..3f6dd12 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,20 @@ return [ */ 'cache_time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'), + /* + * This setting determines if a http header named with the cache age + * should be added to a cached response. This can be handy when + * debugging. + * ONLY works when "add_cache_time_header" is also active! + */ + 'add_cache_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false), + + /* + * This setting determines the name of the http header that contains + * the age of cache + */ + 'cache_age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'), + /* * Here you may define the cache store that should be used to store * requests. This can be the name of any store that is @@ -105,15 +119,14 @@ return [ /* * This class is responsible for generating a hash for a request. This hash - * is used as a key to look up a cached response. + * is used to look up an cached response. */ 'hasher' => \Spatie\ResponseCache\Hasher\DefaultHasher::class, /* - * This class serializes cache data and expands it. - * Serialization can save the data to be returned in an appropriate form. + * This class is responsible for serializing responses. */ - 'serializer' => \Spatie\ResponseCache\Serializer\DefaultSerializer::class, + 'serializer' => \Spatie\ResponseCache\Serializers\DefaultSerializer::class, ]; ``` diff --git a/config/responsecache.php b/config/responsecache.php index d90896e..36df8e2 100644 --- a/config/responsecache.php +++ b/config/responsecache.php @@ -34,6 +34,20 @@ */ 'cache_time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'), + /* + * This setting determines if a http header named with the cache age + * should be added to a cached response. This can be handy when + * debugging. + * ONLY works when "add_cache_time_header" is also active! + */ + 'add_cache_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false), + + /* + * This setting determines the name of the http header that contains + * the age of cache + */ + 'cache_age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'), + /* * Here you may define the cache store that should be used to store * requests. This can be the name of any store that is diff --git a/src/Middlewares/CacheResponse.php b/src/Middlewares/CacheResponse.php index 1813370..3ad8b57 100644 --- a/src/Middlewares/CacheResponse.php +++ b/src/Middlewares/CacheResponse.php @@ -2,6 +2,7 @@ namespace Spatie\ResponseCache\Middlewares; +use Carbon\Carbon; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Collection; @@ -31,6 +32,8 @@ public function handle(Request $request, Closure $next, ...$args): Response $response = $this->responseCache->getCachedResponseFor($request, $tags); + $response = $this->addCacheAgeHeader($response); + $this->getReplacers()->each(function (Replacer $replacer) use ($response) { $replacer->replaceInCachedResponse($response); }); @@ -90,4 +93,15 @@ protected function getTags(array $args): array return array_filter($tags); } + + public function addCacheAgeHeader(Response $response): Response + { + if (config('responsecache.add_cache_age_header') and $time = $response->headers->get(config('responsecache.cache_time_header_name'))) { + $ageInSeconds = Carbon::parse($time)->diffInSeconds(Carbon::now()); + + $response->headers->set(config('responsecache.cache_age_header_name'), $ageInSeconds); + } + + return $response; + } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index a0f6280..4c27342 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -257,4 +257,22 @@ public function it_can_add_a_cache_time_header() $this->assertSameResponse($firstResponse, $secondResponse); } + + /** @test */ + public function it_can_add_a_cache_age_header() + { + $this->app['config']->set('responsecache.add_cache_time_header', true); + $this->app['config']->set('responsecache.add_cache_age_header', true); + $this->app['config']->set('responsecache.cache_age_header_name', 'X-Cached-Age'); + + $firstResponse = $this->get('/random'); + $secondResponse = $this->get('/random'); + + $this->assertFalse($firstResponse->headers->has('X-Cached-Age')); + $this->assertTrue($secondResponse->headers->has('X-Cached-Age')); + + $this->assertIsNumeric($secondResponse->headers->get('X-Cached-Age')); + + $this->assertSameResponse($firstResponse, $secondResponse); + } }