Preventing caching | 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)  Basic-usage  Preventing caching

 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)

 Preventing caching
==================

###  On this page

1. [ Using the DoNotCacheResponse middleware ](#content-using-the-donotcacheresponse-middleware)
2. [ Using the NoCache attribute ](#content-using-the-nocache-attribute)
3. [ Bypassing the cache ](#content-bypassing-the-cache)

Using the DoNotCacheResponse middleware
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can prevent caching for specific routes using the `doNotCacheResponse` middleware.

```
Route::get('/auth/logout', [AuthController::class, 'logout'])
    ->middleware('doNotCacheResponse');
```

Using the NoCache attribute
-----------------------------------------------------------------------------------------------------------------------------------------

You can also disable caching for specific controller methods using the `#[NoCache]` attribute.

```
use Spatie\ResponseCache\Attributes\Cache;
use Spatie\ResponseCache\Attributes\NoCache;

#[Cache(lifetime: 5 * 60)]
class PostController
{
    public function index()
    {
        // This will be cached (inherits class-level attribute)
    }

    #[NoCache]
    public function store()
    {
        // This will not be cached
    }
}
```

Bypassing the cache
-----------------------------------------------------------------------------------------------------------------

You can securely bypass the cache to always receive a fresh response. This is useful for profiling or debugging.

To enable this, set the `CACHE_BYPASS_HEADER_NAME` and `CACHE_BYPASS_HEADER_VALUE` environment variables.

```
CACHE_BYPASS_HEADER_NAME=X-Cache-Bypass
CACHE_BYPASS_HEADER_VALUE=your-secret-value
```

Then include that header when making requests to bypass the cache.
