0
0
Laravelframework~10 mins

Cache tags in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cache tags
Store item with tags
Cache saves item under tags
Retrieve item by tags
Check if item exists
Yes No
Return item
Flush all items with tag
Tagged items removed
Cache tags let you group cached items by labels. You can store, retrieve, or clear all items with the same tag easily.
Execution Sample
Laravel
Cache::tags(['users', 'settings'])->put('user_1', 'John', 3600);
$value = Cache::tags(['users'])->get('user_1');
Cache::tags(['users'])->flush();
Stores 'John' under 'user_1' with tags 'users' and 'settings', retrieves it by 'users' tag, then clears all 'users' tagged cache.
Execution Table
StepActionTags UsedKeyValueCache State
1Put item['users', 'settings']user_1'John'{user_1: 'John' tagged with users, settings}
2Get item['users']user_1'John'{user_1: 'John' tagged with users, settings}
3Flush tag['users']--{}
4Get item after flush['users']user_1null{}
💡 Cache is empty after flushing 'users' tag, so retrieval returns null.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Cache contents{}{user_1: 'John' tagged with users, settings}{user_1: 'John' tagged with users, settings}{}{}
Retrieved valuenull'John''John'nullnull
Key Moments - 2 Insights
Why does flushing the 'users' tag remove the cached item even though it also has the 'settings' tag?
Because cache tags group items, flushing any tag removes all items tagged with it regardless of other tags. See step 3 in execution_table where flushing 'users' clears the item.
What happens if you try to get a cached item with a tag that was not used when storing it?
The item won't be found because tags filter cache keys. You must use at least one tag that matches the stored tags. Step 2 shows retrieval with 'users' tag works because it matches stored tags.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after step 1?
A{}
B{user_1: 'John' tagged with users, settings}
C{user_1: 'John' tagged with users}
Dnull
💡 Hint
Check the 'Cache State' column in row for step 1.
At which step does the cache become empty?
AStep 2
BStep 4
CStep 3
DNever
💡 Hint
Look at the 'Cache State' column and see when it changes to {}.
If you flush the 'settings' tag instead of 'users' at step 3, what happens to the cache?
ACache is emptied
BCache still contains the item
CCache contains a different item
DError occurs
💡 Hint
Since the item is tagged with both 'users' and 'settings', flushing either tag removes it.
Concept Snapshot
Cache tags group cached items by labels.
Use Cache::tags(['tag1', 'tag2'])->put(key, value, ttl) to store.
Retrieve with Cache::tags(['tag1'])->get(key).
Flush all items with a tag using Cache::tags(['tag1'])->flush().
Flushing a tag removes all items with that tag, even if they have other tags.
Full Transcript
Cache tags in Laravel let you label cached items with one or more tags. When you store an item, you specify tags. Later, you can retrieve the item by using any of its tags. You can also clear all cached items that share a tag by flushing that tag. For example, storing 'John' under key 'user_1' with tags 'users' and 'settings' means the item belongs to both groups. Retrieving with tag 'users' finds it. Flushing 'users' tag removes it from cache, even though it also had 'settings' tag. This grouping helps manage cache efficiently by clearing related items together.