|
10 | 10 | - [Removing Items From the Cache](#removing-items-from-the-cache) |
11 | 11 | - [Cache Memoization](#cache-memoization) |
12 | 12 | - [The Cache Helper](#the-cache-helper) |
| 13 | +- [Cache Tags](#cache-tags) |
13 | 14 | - [Atomic Locks](#atomic-locks) |
14 | 15 | - [Managing Locks](#managing-locks) |
15 | 16 | - [Managing Locks Across Processes](#managing-locks-across-processes) |
@@ -398,6 +399,42 @@ cache()->remember('users', $seconds, function () { |
398 | 399 | > [!NOTE] |
399 | 400 | > When testing calls to the global `cache` function, you may use the `Cache::shouldReceive` method just as if you were [testing the facade](/docs/{{version}}/mocking#mocking-facades). |
400 | 401 |
|
| 402 | +<a name="cache-tags"></a> |
| 403 | +## Cache Tags |
| 404 | + |
| 405 | +> [!WARNING] |
| 406 | +> Cache tags are not supported when using the `file`, `dynamodb`, or `database` cache drivers. |
| 407 | +
|
| 408 | +<a name="storing-tagged-cache-items"></a> |
| 409 | +### Storing Tagged Cache Items |
| 410 | + |
| 411 | +Cache tags allow you to tag related items in the cache and then flush all cached values that have been assigned a given tag. You may access a tagged cache by passing in an ordered array of tag names. For example, let's access a tagged cache and `put` a value into the cache: |
| 412 | + |
| 413 | + use Illuminate\Support\Facades\Cache; |
| 414 | + |
| 415 | + Cache::tags(['people', 'artists'])->put('John', $john, $seconds); |
| 416 | + Cache::tags(['people', 'authors'])->put('Anne', $anne, $seconds); |
| 417 | + |
| 418 | +<a name="accessing-tagged-cache-items"></a> |
| 419 | +### Accessing Tagged Cache Items |
| 420 | + |
| 421 | +Items stored via tags may not be accessed without also providing the tags that were used to store the value. To retrieve a tagged cache item, pass the same ordered list of tags to the `tags` method, then call the `get` method with the key you wish to retrieve: |
| 422 | + |
| 423 | + $john = Cache::tags(['people', 'artists'])->get('John'); |
| 424 | + |
| 425 | + $anne = Cache::tags(['people', 'authors'])->get('Anne'); |
| 426 | + |
| 427 | +<a name="removing-tagged-cache-items"></a> |
| 428 | +### Removing Tagged Cache Items |
| 429 | + |
| 430 | +You may flush all items that are assigned a tag or list of tags. For example, the following code would remove all caches tagged with either `people`, `authors`, or both. So, both `Anne` and `John` would be removed from the cache: |
| 431 | + |
| 432 | + Cache::tags(['people', 'authors'])->flush(); |
| 433 | + |
| 434 | +In contrast, the code below would remove only cached values tagged with `authors`, so `Anne` would be removed, but not `John`: |
| 435 | + |
| 436 | + Cache::tags('authors')->flush(); |
| 437 | + |
401 | 438 | <a name="atomic-locks"></a> |
402 | 439 | ## Atomic Locks |
403 | 440 |
|
|
0 commit comments