Performance: Storing and retrieving cache
This affects page load speed by reducing database queries and computation time through fast data retrieval from cache.
Jump into concepts and practice - no test required
<?php $data = Cache::remember('users', 3600, fn() => DB::table('users')->get()); // Cache stores result for 60 minutes
<?php
$data = DB::table('users')->get();
// No caching, query runs on every request
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cache, direct DB query | N/A | N/A | Blocks rendering waiting for server response | [X] Bad |
| Cache with expiration and proper key | N/A | N/A | Fast server response enables quicker paint | [OK] Good |
Cache::put('key', 'value', 60) method do in Laravel?Cache::put method stores data in the cache with a key and expiration time in minutes.Cache::get to retrieve cached data safely.Cache::put('count', 5, 10);
$value = Cache::get('count', 0);
echo $value;Cache::put('user', 'Alice');
$value = Cache::get('user');
echo $value;Cache::forever to store data without expiration.Cache::forget to delete the cached key when needed.