08.10.2017 Views

codeigniter_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

18. Page Caching<br />

CodeIgniter<br />

Caching a page will improve the page load speed. If the page is cached, then it will be<br />

stored in its fully rendered state. Next time, when the server gets a request for the<br />

cached page, it will be directly sent to the requested browser.<br />

Cached files are stored in application/cache folder. Caching can be enabled on per<br />

page basis. While enabling the cache, we need to set the time, until which it needs to<br />

remain in cached folder and after that period, it will be deleted automatically.<br />

Enable Caching<br />

Caching can be enabled by executing the following line in any of the controller’s method.<br />

$this->output->cache($n);<br />

Where $n is the number of minutes, you wish the page to remain cached between<br />

refreshes.<br />

Disable Caching<br />

Cache file gets deleted when it expires but when you want to delete it manually, then<br />

you have to disable it. You can disable the caching by executing the following line.<br />

// Deletes cache for the currently requested URI<br />

$this->output->delete_cache();<br />

// Deletes cache for /foo/bar<br />

$this->output->delete_cache('/foo/bar');<br />

Example<br />

Create a controller called Cache_controller.php and save it in<br />

application/controller/Cache_controller.php<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!