Consider the following Laravel cache code:
Cache::tags(['users', 'settings'])->put('config', 'value1', 3600);
Cache::tags(['users'])->flush();
$value = Cache::tags(['users', 'settings'])->get('config');
echo $value ?? 'null';What will be printed?
Cache::tags(['users', 'settings'])->put('config', 'value1', 3600); Cache::tags(['users'])->flush(); $value = Cache::tags(['users', 'settings'])->get('config'); echo $value ?? 'null';
Think about what happens when you flush a tag and how it affects cache items with multiple tags.
Flushing the 'users' tag removes all cache items tagged with 'users', including those tagged with both 'users' and 'settings'. So the item 'config' is removed and returns null.
Which of the following code snippets correctly stores the value 'blue' with tags 'colors' and 'themes' for 10 minutes?
Remember how to pass multiple tags as an array in Laravel's cache tagging.
The tags method accepts an array of tags. Chaining multiple calls or passing array of arrays is incorrect.
Given this code:
Cache::tags(['session', 'user'])->put('token', 'abc123', 300);
// ... some time later
$token = Cache::tags(['user', 'session'])->get('token');
echo $token ?? 'null';The output is 'null'. Why?
Cache::tags(['session', 'user'])->put('token', 'abc123', 300); $token = Cache::tags(['user', 'session'])->get('token'); echo $token ?? 'null';
Notice that the order of the tags is reversed between put and get.
Cache tags order matters because the tags array is serialized to compute the tag root key hash. Different order produces different prefix for the item key, so retrieval fails.
Consider these cache operations:
Cache::tags(['a', 'b'])->put('item1', 'x', 100);
Cache::tags(['a'])->put('item2', 'y', 100);
Cache::tags(['b'])->put('item3', 'z', 100);
Cache::tags(['a'])->flush();
$count = Cache::tags(['b'])->getMultiple(['item1', 'item3']);
$itemsCount = count(array_filter($count));
echo $itemsCount;What number is printed?
Cache::tags(['a', 'b'])->put('item1', 'x', 100); Cache::tags(['a'])->put('item2', 'y', 100); Cache::tags(['b'])->put('item3', 'z', 100); Cache::tags(['a'])->flush(); $count = Cache::tags(['b'])->getMultiple(['item1', 'item3']); $itemsCount = count(array_filter($count)); echo $itemsCount;
Think about which items are removed when flushing tag 'a' and which remain under tag 'b'.
Flushing tag 'a' removes items tagged with 'a'. 'item1' has tags 'a' and 'b' so it is removed. 'item2' has 'a' only, removed. 'item3' has 'b' only, remains. So only 'item3' is returned.
Choose the correct statement about Laravel cache tags:
Think about how flushing a tag affects items with multiple tags and driver support.
Flushing a tag removes all items with that tag, even if they have other tags. Not all drivers support tags (e.g., file, database). Tags are flat, not hierarchical. Cache keys can be reused under different tags.