Challenge 2: Approximate Full-Response Caching — Possible Solution ==================================================================== // app/Http/Controllers/BookController.php use Illuminate\Support\Facades\Cache; use App\Models\Book; public function index() { $books = Cache::remember('books.index.data', 900, function () { return Book::with('author')->get(); }); return view('books.index', ['books' => $books]); } HOW THIS DIFFERS FROM DJANGO'S @cache_page ----------------------------------------------- This caches only the QUERY RESULT (the Eloquent Collection of books) for 15 minutes — every request within that window still runs the full Blade template rendering process (view('books.index', [...])) fresh, even though the underlying data came from the cache instead of the database. Django's @cache_page decorator, by contrast, caches the ENTIRE FINISHED HTTP RESPONSE — meaning a cached request skips template rendering entirely and returns the exact same bytes that were computed the first time, making it a strictly cheaper operation per request than this Laravel approach, which still pays the Blade-rendering cost every time even on a "cache hit" for the data. WHY THIS WORKS AS AN APPROXIMATION -------------------------------------- - The expensive part — the database query (and any N+1-prone relationship loading via with('author')) — is exactly what gets skipped on repeated requests, which is usually where the real cost lives for a database-backed page. - Achieving Django's TRUE response-level caching in Laravel would require either a dedicated package (like spatie/laravel-responsecache, which wraps the whole response object, not just data) or manually caching the fully-rendered HTML string itself (e.g. Cache::remember('books.index.html', 900, fn() => view('books.index', [...])->render())) — the latter approximates Django's behavior more closely, at the cost of losing per-request dynamic content (like a CSRF token or a personalized greeting) unless handled carefully, exactly the same trade-off Django's template fragment caching chapter warned about for content that must stay fresh per-request.