0
0
Laravelframework~20 mins

Cache tags in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Cache Tags Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when retrieving a tagged cache item after flushing one tag?

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?

Laravel
Cache::tags(['users', 'settings'])->put('config', 'value1', 3600);
Cache::tags(['users'])->flush();
$value = Cache::tags(['users', 'settings'])->get('config');
echo $value ?? 'null';
Avalue1
Bfalse
Cnull
DThrows an exception
Attempts:
2 left
💡 Hint

Think about what happens when you flush a tag and how it affects cache items with multiple tags.

📝 Syntax
intermediate
2:00remaining
Which option correctly stores a value with multiple cache tags in Laravel?

Which of the following code snippets correctly stores the value 'blue' with tags 'colors' and 'themes' for 10 minutes?

ACache::tags('colors')->tags('themes')->put('background', 'blue', 600);
BCache::tags(['colors', 'themes'])->put('background', 'blue', 600);
CCache::tags(['colors'], ['themes'])->put('background', 'blue', 600);
DCache::tags('colors', 'themes')->put('background', 'blue', 600);
Attempts:
2 left
💡 Hint

Remember how to pass multiple tags as an array in Laravel's cache tagging.

🔧 Debug
advanced
2:00remaining
Why does this tagged cache retrieval return null unexpectedly?

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?

Laravel
Cache::tags(['session', 'user'])->put('token', 'abc123', 300);
$token = Cache::tags(['user', 'session'])->get('token');
echo $token ?? 'null';
ACache tags order matters; ['user', 'session'] is different from ['session', 'user']
BThe cache key 'token' was never stored
CCache tags are case-sensitive and tags differ in case
DThe cache store does not support tagging
Attempts:
2 left
💡 Hint

Notice that the order of the tags is reversed between put and get.

state_output
advanced
2:00remaining
What is the number of items in cache after flushing one tag?

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?

Laravel
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;
A3
B2
C0
D1
Attempts:
2 left
💡 Hint

Think about which items are removed when flushing tag 'a' and which remain under tag 'b'.

🧠 Conceptual
expert
2:00remaining
Which statement about Laravel cache tags is true?

Choose the correct statement about Laravel cache tags:

ACache tags allow grouping cache items so flushing one tag removes only items with that tag, even if they have multiple tags.
BCache tags require the cache key to be unique across all tags to avoid collisions.
CCache tags can be nested to create hierarchical cache groups automatically.
DCache tags are supported by all Laravel cache drivers including 'file' and 'database'.
Attempts:
2 left
💡 Hint

Think about how flushing a tag affects items with multiple tags and driver support.