Skip to content

Commit

Permalink
Add option to output cache age header (#385)
Browse files Browse the repository at this point in the history
* Add option to output cache age header

* fix config

* extract method

* update readme
  • Loading branch information
it-can authored May 16, 2022
1 parent 1e54ef4 commit aa44b99
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
];
```

Expand Down
14 changes: 14 additions & 0 deletions config/responsecache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Middlewares/CacheResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\ResponseCache\Middlewares;

use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit aa44b99

Please sign in to comment.