What is output caching vs data caching in CodeIgniter 4?
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.
Previous
What is CI4 model findAll() with conditions?
Next
What is the difference between CI4 redirect()->to() and redirect()->back()?