What is output caching vs data caching in CodeIgniter 4?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

CI4 supports two levels of caching. Data caching stores computed values (query results, API responses, processed data) in a cache store (file, Redis, Memcached) using the Cache service: if (!$data = cache("key")) { $data = $this->model->expensive(); cache()->save("key", $data, 600); }. This caches data between requests while the controller still runs. Page/Output caching stores the complete rendered HTML response and serves it as a static file, bypassing the controller entirely for cached requests. Enable in a controller method: $this->cachePage(300). The first request renders the page and saves it to writable/cache/. Subsequent requests within 300 seconds serve the file directly. Clear specific page cache: cache()->deleteMatching("url_hash*"). Use output caching for truly static pages (landing pages, blog posts that rarely change); use data caching for pages that need some dynamic elements but have expensive data operations.

Pro Tip

If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.