Bird
Raised Fist0
Laravelframework~20 mins

Cache tags in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using cache tags in Laravel?
easy
A. To group cached items for easy management and selective clearing
B. To encrypt cached data automatically
C. To increase cache storage size
D. To log cache hits and misses

Solution

  1. Step 1: Understand cache tags concept

    Cache tags allow grouping of cached items under one or more tags.
  2. Step 2: Identify the benefit of grouping

    This grouping helps to clear or manage all related cache items easily by tag.
  3. Final Answer:

    To group cached items for easy management and selective clearing -> Option A
  4. Quick Check:

    Cache tags = group and manage cache [OK]
Hint: Cache tags group items for easy clearing [OK]
Common Mistakes:
  • Thinking tags encrypt data
  • Assuming tags increase cache size
  • Confusing tags with logging features
2. Which of the following is the correct syntax to store a cache item with a tag in Laravel?
easy
A. Cache::tags('users')->put('user_1', 'John', 3600);
B. Cache::put('user_1', 'John')->tags(['users'], 3600);
C. Cache::put('user_1', 'John', 3600)->tags(['users']);
D. Cache::tags(['users'])->put('user_1', 'John', 3600);

Solution

  1. Step 1: Recall Laravel cache tag syntax

    The tags() method accepts an array of tags before calling put().
  2. Step 2: Check method chaining order

    The correct order is Cache::tags([...])->put(key, value, time).
  3. Final Answer:

    Cache::tags(['users'])->put('user_1', 'John', 3600); -> Option D
  4. Quick Check:

    tags() before put() with array [OK]
Hint: Use tags() with array before put() [OK]
Common Mistakes:
  • Passing tags after put()
  • Using string instead of array in tags()
  • Chaining methods in wrong order
3. Given the code below, what will be the output of Cache::tags(['products'])->get('item_1'); after Cache::tags(['products'])->flush(); is called?
Cache::tags(['products'])->put('item_1', 'Book', 600);
Cache::tags(['products'])->flush();
$value = Cache::tags(['products'])->get('item_1');
echo $value ?? 'Not found';
medium
A. Not found
B. CacheException
C. null
D. Book

Solution

  1. Step 1: Understand flush() effect on tagged cache

    Calling flush() on a tag clears all cached items under that tag.
  2. Step 2: Check retrieval after flush

    After flushing, get('item_1') returns null, so the null coalescing operator outputs 'Not found'.
  3. Final Answer:

    Not found -> Option A
  4. Quick Check:

    flush() clears tagged cache = 'Not found' [OK]
Hint: flush() clears all tagged cache items [OK]
Common Mistakes:
  • Expecting cached value after flush
  • Confusing flush() with forget()
  • Assuming flush() only clears one item
4. Identify the error in the following Laravel cache tag usage:
Cache::tags('sessions')->put('session_1', 'data', 120);
Cache::tags(['sessions'])->flush();
medium
A. Missing cache driver configuration
B. Using string instead of array in tags() method
C. Calling flush() on tags is not allowed
D. put() method requires a closure

Solution

  1. Step 1: Check tags() method argument type

    The tags() method requires an array of tags, not a string.
  2. Step 2: Identify the incorrect usage

    Passing a string 'sessions' instead of ['sessions'] causes an error.
  3. Final Answer:

    Using string instead of array in tags() method -> Option B
  4. Quick Check:

    tags() needs array, not string [OK]
Hint: Always pass array to tags() [OK]
Common Mistakes:
  • Passing single string instead of array
  • Thinking flush() is disallowed
  • Assuming put() needs closure
5. You want to cache user profile data and product data separately but clear both caches together when needed. Which is the best way to use cache tags in Laravel to achieve this?
hard
A. Tag user profiles with ['users'] and products with ['products'], then flush both tags separately
B. Use no tags and clear cache manually by keys
C. Tag both user profiles and products with ['app_data'], then flush this tag to clear all
D. Tag user profiles with ['users'] and products with ['products'], then flush(['users', 'products'])

Solution

  1. Step 1: Understand grouping with tags

    To clear multiple cache groups together, use a common tag for both.
  2. Step 2: Apply a shared tag

    Tag both user and product caches with a shared tag like 'app_data' to flush all at once.
  3. Final Answer:

    Tag both user profiles and products with ['app_data'], then flush this tag to clear all -> Option C
  4. Quick Check:

    Shared tag groups caches for joint flush [OK]
Hint: Use shared tag to clear multiple caches together [OK]
Common Mistakes:
  • Flushing tags separately instead of shared tag
  • Not tagging caches for group clearing
  • Trying to flush multiple tags in one call incorrectly