Skip to content

Commit 35614d0

Browse files
committed
re-document cache tags
1 parent 407c91a commit 35614d0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

cache.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Removing Items From the Cache](#removing-items-from-the-cache)
1111
- [Cache Memoization](#cache-memoization)
1212
- [The Cache Helper](#the-cache-helper)
13+
- [Cache Tags](#cache-tags)
1314
- [Atomic Locks](#atomic-locks)
1415
- [Managing Locks](#managing-locks)
1516
- [Managing Locks Across Processes](#managing-locks-across-processes)
@@ -398,6 +399,42 @@ cache()->remember('users', $seconds, function () {
398399
> [!NOTE]
399400
> 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).
400401
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+
401438
<a name="atomic-locks"></a>
402439
## Atomic Locks
403440

0 commit comments

Comments
 (0)