0
0
Laravelframework~20 mins

Storing and retrieving cache in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
A15
Bnull
CThrows an exception
D0
Attempts:
2 left
💡 Hint
Think about what Cache::put and Cache::get do in Laravel.
📝 Syntax
intermediate
2: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?
ACache::put('greeting', 'hello', 5);
BCache::put('greeting', 'hello', now()->addMinutes(5));
CCache::put('greeting', 'hello', 5 * 60);
DCache::put('greeting', 'hello', '5 minutes');
Attempts:
2 left
💡 Hint
Laravel cache expiration can be a DateTime or a number of seconds.
🔧 Debug
advanced
2: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');
AThe cache driver does not support storing arrays.
BThe cache key was never set, so it returns null.
CThe cache expired after 1 second, so after 2 minutes the value is null.
DCache::get always returns null for arrays.
Attempts:
2 left
💡 Hint
Think about the expiration time and when the retrieval happens.
🧠 Conceptual
advanced
2: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?
AIt always runs expensiveFunction() and ignores the cache.
BIt returns the cached value if it exists; otherwise, it runs expensiveFunction(), caches the result for 10 minutes, and returns it.
CIt caches the closure itself instead of the result.
DIt throws an error because closures cannot be cached.
Attempts:
2 left
💡 Hint
Cache::remember is designed to cache the result of the closure.
state_output
expert
2: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;
AThrows an error because increment requires an integer
B8
C5
D9
Attempts:
2 left
💡 Hint
Increment adds to the cached value each time.