Web Page Caching
CodeIgniter lets you cache your pages in order to achieve maximumperformance.
Although CodeIgniter is quite fast, the amount of dynamic informationyou display in your pages will correlate directly to the serverresources, memory, and processing cycles utilized, which affect yourpage load speeds. By caching your pages, since they are saved in theirfully rendered state, you can achieve performance much closer to that ofstatic web pages.
How Does Caching Work?
Caching can be enabled on a per-page basis, and you can set the lengthof time that a page should remain cached before being refreshed. When apage is loaded for the first time, the file will be cached using thecurrently configured cache engine. On subsequent page loads, the cache filewill be retrieved and sent to the requesting user’s browser. If it hasexpired, it will be deleted and refreshed before being sent to thebrowser.
Note
The Benchmark tag is not cached so you can still view your pageload speed when caching is enabled.
Enabling Caching
To enable caching, put the following tag in any of your controllermethods:
- $this->cachePage($n);
Where $n
is the number of seconds you wish the page to remaincached between refreshes.
The above tag can go anywhere within a method. It is not affected bythe order that it appears, so place it wherever it seems most logical toyou. Once the tag is in place, your pages will begin being cached.
Important
If you change configuration options that might affectyour output, you have to manually delete your cache files.
Note
Before the cache files can be written you must set the cacheengine up by editing app/Config/Cache.php.
Deleting Caches
If you no longer wish to cache a file you can remove the caching tag andit will no longer be refreshed when it expires.
Note
Removing the tag will not delete the cache immediately. It willhave to expire normally.