Using tags | 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  Using tags

 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)

 Using tags
==========

###  On this page

1. [ Tagging with middleware ](#content-tagging-with-middleware)
2. [ Tagging with attributes ](#content-tagging-with-attributes)
3. [ Global tags ](#content-global-tags)
4. [ Clearing by tag ](#content-clearing-by-tag)

If the [cache driver you configured supports tags](https://laravel.com/docs/12.x/cache#cache-tags), you can tag cached responses. This allows you to clear specific groups of cached responses without clearing the entire cache.

Tagging with middleware
-----------------------------------------------------------------------------------------------------------------------------

```
use Spatie\ResponseCache\Middlewares\CacheResponse;

Route::get('/posts', [PostController::class, 'index'])
    ->middleware(CacheResponse::for(minutes(5), tags: ['posts']));

Route::get('/api/posts', [ApiPostController::class, 'index'])
    ->middleware(CacheResponse::for(minutes(5), tags: ['posts', 'api']));
```

Tagging with attributes
-----------------------------------------------------------------------------------------------------------------------------

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

class PostController
{
    #[Cache(lifetime: 5 * 60, tags: ['posts'])]
    public function index() { /* ... */ }

    #[Cache(lifetime: 5 * 60, tags: ['posts', 'api'])]
    public function apiIndex() { /* ... */ }
}
```

Global tags
-----------------------------------------------------------------------------------------

You can also set a global tag in the config file. All cached responses will receive this tag.

```
// config/responsecache.php

'cache' => [
    'tag' => 'responsecache',
    // or multiple tags:
    'tag' => ['responsecache', 'pages'],
],
```

Clearing by tag
-----------------------------------------------------------------------------------------------------

You can clear responses that are assigned a specific tag.

```
use Spatie\ResponseCache\Facades\ResponseCache;

// Clear only responses tagged with 'posts'
ResponseCache::clear(['posts']);
```

This uses [Laravel's built-in cache tags](https://laravel.com/docs/12.x/cache#cache-tags) functionality.
