Cache helps your app remember data so it can load faster next time. It saves time by not doing the same work again.
Storing and retrieving cache in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
<?php use Illuminate\Support\Facades\Cache; // Store data in cache Cache::put('key', 'value', $seconds); // Retrieve data from cache $value = Cache::get('key', 'default'); // Store data forever Cache::forever('key', 'value'); // Remove data from cache Cache::forget('key');
Use Cache::put to save data with a time limit in seconds or as a DateTime instance.
Use Cache::get to get data; it returns a default if the key is missing.
<?php Cache::put('username', 'alice', 3600); // Store 'alice' for 1 hour $name = Cache::get('username');
<?php $value = Cache::get('non_existing_key', 'guest');
<?php Cache::forever('site_name', 'My Website'); $name = Cache::get('site_name');
<?php Cache::forget('username'); $name = Cache::get('username', 'not found');
This program shows how to store data temporarily and permanently in cache, retrieve it, and handle expiration and removal.
<?php use Illuminate\Support\Facades\Cache; // Store a value in cache for 10 seconds Cache::put('greeting', 'Hello, Laravel!', 10); // Retrieve and print the cached value echo "Cached greeting: " . Cache::get('greeting', 'No greeting found') . "\n"; // Wait 11 seconds to let cache expire sleep(11); // Try to get the expired cache echo "After expiration: " . Cache::get('greeting', 'Cache expired') . "\n"; // Store a value forever Cache::forever('welcome', 'Welcome to the site!'); // Retrieve and print the forever cached value echo "Forever cached message: " . Cache::get('welcome') . "\n"; // Remove the forever cached value Cache::forget('welcome'); // Try to get the removed cache echo "After removal: " . Cache::get('welcome', 'No message found') . "\n";
Cache::put has time complexity O(1) for storing data.
Cache::get also works in O(1) time to retrieve data.
Common mistake: forgetting to set expiration time, which uses the default TTL from config.
Use cache for data that is expensive to get or compute but changes occasionally.
Cache stores data to speed up your app by avoiding repeated work.
Use Cache::put to save with expiration, Cache::get to retrieve safely with default.
Use Cache::forever for permanent storage and Cache::forget to remove data.
Practice
Cache::put('key', 'value', 60) method do in Laravel?Solution
Step 1: Understand Cache::put method
TheCache::putmethod stores data in the cache with a key and expiration time in minutes.Step 2: Analyze the parameters
Here, 'key' is the cache key, 'value' is the data stored, and 60 means it expires after 60 minutes.Final Answer:
Stores 'value' in cache under 'key' for 60 minutes -> Option DQuick Check:
Cache::put stores with expiration = A [OK]
- Confusing expiration time units (seconds vs minutes)
- Thinking Cache::put retrieves data
- Assuming Cache::put stores data permanently
Solution
Step 1: Identify Laravel cache retrieval method
Laravel usesCache::getto retrieve cached data safely.Step 2: Check method parameters
The second parameter is the default value returned if the key is not found.Final Answer:
Cache::get('key', 'default') -> Option AQuick Check:
Cache::get with default = A [OK]
- Using non-existent methods like retrieve or fetch
- Forgetting the default fallback parameter
- Using Cache::put instead of Cache::get
Cache::put('count', 5, 10);
$value = Cache::get('count', 0);
echo $value;Solution
Step 1: Store value in cache
The code stores the integer 5 under the key 'count' with expiration 10 minutes.Step 2: Retrieve value from cache
Then it retrieves 'count' with default 0. Since the key exists, it returns 5.Final Answer:
5 -> Option CQuick Check:
Cache::get returns stored value if exists = B [OK]
- Assuming default value is always returned
- Confusing expiration time with immediate deletion
- Expecting null or error instead of stored value
Cache::put('user', 'Alice');
$value = Cache::get('user');
echo $value;Solution
Step 1: Check Cache::put usage
Cache::put requires three parameters: key, value, and expiration time in minutes.Step 2: Identify missing expiration
The code misses the expiration time, so it will cause an error or unexpected behavior.Final Answer:
Cache::put requires expiration time as third argument -> Option AQuick Check:
Cache::put needs expiration time = D [OK]
- Omitting expiration time in Cache::put
- Thinking Cache::get always needs default
- Believing keys must be numeric
Solution
Step 1: Store data permanently
UseCache::foreverto store data without expiration.Step 2: Remove cached data
UseCache::forgetto delete the cached key when needed.Final Answer:
Cache::forever('settings', $data); Cache::forget('settings'); -> Option BQuick Check:
Permanent store + forget = C [OK]
- Using wrong method names like delete or remove
- Passing 0 as expiration to put for permanent storage
- Confusing Cache::store or Cache::save as valid methods
