Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to store a value in Laravel cache.
Laravel
Cache::put('key', 'value', [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for duration.
Passing null which means no caching.
✗ Incorrect
The third parameter is the number of minutes to cache the value. Using 60 caches it for one hour.
2fill in blank
mediumComplete the code to retrieve a cached value or run a closure if not cached.
Laravel
$value = Cache::remember('key', 30, function() { return [1]; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null which caches nothing.
Calling Cache::get inside the closure causing recursion.
✗ Incorrect
The closure returns the value to cache if the key is missing. Here, it returns 'default value'.
3fill in blank
hardFix the error in the code to check if a cache key exists.
Laravel
if (Cache::[1]('user_123')) { return Cache::get('user_123'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exists' which is not a Laravel Cache method.
Using 'contains' or 'check' which do not exist.
✗ Incorrect
The correct method to check if a cache key exists is has.
4fill in blank
hardFill both blanks to cache a database query result for 10 minutes.
Laravel
$users = Cache::[1]('users', [2], function() { return DB::table('users')->get(); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'put' which does not return the cached value.
Using 60 minutes instead of 10.
✗ Incorrect
Use remember to cache the result for 10 minutes.
5fill in blank
hardFill all three blanks to remove a cache key and then check if it still exists.
Laravel
Cache::[1]('session_45'); if (!Cache::[2]('session_45')) { return '[3]'; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exists' instead of 'has' to check key.
Returning wrong message string.
✗ Incorrect
forget removes the cache key, has checks existence, and 'deleted' is the message returned.