Challenge - 5 Problems
Laravel Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel cache retrieval code?
Consider this Laravel code snippet that stores and retrieves a cache value. What will be the output when the code runs?
Laravel
<?php use Illuminate\Support\Facades\Cache; Cache::put('user_count', 15, 10); $value = Cache::get('user_count'); echo $value;
Attempts:
2 left
💡 Hint
Think about what Cache::put and Cache::get do in Laravel.
✗ Incorrect
The code stores the value 15 in the cache with the key 'user_count' for 10 seconds. Then it retrieves the value using Cache::get. Since the key exists, it returns 15.
📝 Syntax
intermediate2:00remaining
Which option correctly stores a value in Laravel cache for 5 minutes?
You want to store the string 'hello' in the cache with the key 'greeting' for 5 minutes. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Laravel cache expiration can be a DateTime or a number of seconds.
✗ Incorrect
Options B and C are correct. B uses a DateTime instance (now()->addMinutes(5)). C uses 300 seconds (5 * 60 = 5 minutes). Option A uses 5 seconds (too short). Option D uses invalid string syntax.
🔧 Debug
advanced2:00remaining
Why does this Laravel cache retrieval return null?
Given this code, why does Cache::get('session_data') return null?
Laravel
<?php use Illuminate\Support\Facades\Cache; Cache::put('session_data', ['id' => 1, 'name' => 'Alice'], 1); $value = Cache::get('session_data'); // After 2 minutes $valueAfter = Cache::get('session_data');
Attempts:
2 left
💡 Hint
Think about the expiration time and when the retrieval happens.
✗ Incorrect
The cache was set to expire after 1 second. Retrieving after 2 minutes means the cache expired and returns null.
🧠 Conceptual
advanced2:00remaining
What happens if you use Cache::remember with a closure in Laravel?
What is the behavior of Cache::remember('key', 10, fn() => expensiveFunction()) in Laravel?
Attempts:
2 left
💡 Hint
Cache::remember is designed to cache the result of the closure.
✗ Incorrect
Cache::remember checks if the key exists. If yes, returns cached value. If no, runs the closure, caches its return value, then returns it.
❓ state_output
expert2:00remaining
What is the output of this Laravel cache increment code?
Given this code, what is the final value of $count after execution?
Laravel
<?php use Illuminate\Support\Facades\Cache; Cache::put('count', 5, 10); Cache::increment('count'); Cache::increment('count', 3); $count = Cache::get('count'); echo $count;
Attempts:
2 left
💡 Hint
Increment adds to the cached value each time.
✗ Incorrect
Initial value is 5. First increment adds 1 (default), second adds 3, total 5 + 1 + 3 = 9.