CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
Page Caching in CodeIgniter is a simple way to improve performance and speed of your website.
When caching is enabled, CodeIgniter saves the output of a page request as a static file.
Next time when the same page is requested, CodeIgniter will serve the cached version instead of processing the entire controller and database queries again.
This reduces:
application/cache/
Use the cache()
method in your controller:
class Welcome extends CI_Controller { public function index() { // Enable cache for 10 minutes $this->output->cache(10); $this->load->view('welcome_message'); } }
โ Here, the page output will be cached for 10 minutes.
To clear cache for a specific URL, you can manually delete the cache files inside:
application/cache/
Or use helper functions (in CI3 you often write a custom function for cache removal). Example:
$this->output->delete_cache('welcome/index');
Cached files are stored in:
application/cache/
Make sure this folder is writable by the server.
?id=1
and ?id=2
, they will generate different cache files.class Blog extends CI_Controller { public function article($id) { // Cache for 30 minutes $this->output->cache(30); $data['article'] = $this->db->get_where('articles', ['id' => $id])->row(); $this->load->view('blog_view', $data); } }
๐ Now, when a user visits the blog page, it will be cached for 30 minutes.