You can install the package via composer.
composer require spatie/laravel-responsecache
The package will automatically register itself.
##Registering the middleware
To get started, add the CacheResponse middleware and the DoNotCacheResponse alias in bootstrap/app.php.
use Spatie\ResponseCache\Middlewares\CacheResponse;
use Spatie\ResponseCache\Middlewares\DoNotCacheResponse;
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
CacheResponse::class,
]);
$middleware->alias([
'doNotCacheResponse' => DoNotCacheResponse::class,
]);
})
By default, the package will now cache all successful GET requests that return text based content (such as HTML and JSON) for a week.
##Publishing the config file
Optionally, you can publish the config file.
php artisan vendor:publish --tag="responsecache-config"
The published config file looks like this.
return [
'enabled' => env('RESPONSE_CACHE_ENABLED', true),
'cache' => [
'store' => env('RESPONSE_CACHE_DRIVER', 'file'),
'lifetime_in_seconds' => (int) env('RESPONSE_CACHE_LIFETIME', 60 * 60 * 24 * 7),
'tag' => env('RESPONSE_CACHE_TAG', ''),
],
'bypass' => [
'header_name' => env('CACHE_BYPASS_HEADER_NAME'),
'header_value' => env('CACHE_BYPASS_HEADER_VALUE'),
],
'debug' => [
'enabled' => env('APP_DEBUG', false),
'cache_time_header_name' => 'X-Cache-Time',
'cache_status_header_name' => 'X-Cache-Status',
'cache_age_header_name' => 'X-Cache-Age',
'cache_key_header_name' => 'X-Cache-Key',
],
'ignored_query_parameters' => [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_term',
'utm_content',
'gclid',
'fbclid',
],
'cache_profile' => Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests::class,
'hasher' => \Spatie\ResponseCache\Hasher\DefaultHasher::class,
'serializer' => \Spatie\ResponseCache\Serializers\JsonSerializer::class,
'replacers' => [
\Spatie\ResponseCache\Replacers\CsrfTokenReplacer::class,
],
];