Installation &amp; setup | laravel-responsecache | Spatie

 SPATIE

  Laravel Response Cache
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-responsecache](https://spatie.be/docs/laravel-responsecache/v8)  Installation &amp; setup

 Version   v8

 Other versions for crawler [v8](https://spatie.be/docs/laravel-responsecache/v8)

- [ Introduction ](https://spatie.be/docs/laravel-responsecache/v8/introduction)
- [ Support us ](https://spatie.be/docs/laravel-responsecache/v8/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-responsecache/v8/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-responsecache/v8/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-responsecache/v8/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-responsecache/v8/changelog)
- [ About us ](https://spatie.be/docs/laravel-responsecache/v8/about-us)

Basic usage
-----------

- [ Caching responses ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/caching-responses)
- [ Flexible caching (SWR) ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/flexible-caching)
- [ Preventing caching ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/preventing-caching)
- [ Clearing the cache ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/clearing-the-cache)
- [ Using tags ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/using-tags)
- [ Events ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/events)

Advanced usage
--------------

- [ Custom cache profiles ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/custom-cache-profiles)
- [ Creating a replacer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/creating-a-replacer)
- [ Customizing the serializer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-serializer)
- [ Customizing the hasher ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-hasher)
- [ Configuration ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/configuration)

 Installation &amp; setup
========================

###  On this page

1. [ Registering the middleware ](#content-registering-the-middleware)
2. [ Publishing the config file ](#content-publishing-the-config-file)

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`.

```
// 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,
    ],
];
```
