blob: c40f652acd078f1acee92365e1f79e0f6815ed22 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Web Page Caching
3################
4
5CodeIgniter lets you cache your pages in order to achieve maximum
6performance.
7
8Although CodeIgniter is quite fast, the amount of dynamic information
9you display in your pages will correlate directly to the server
10resources, memory, and processing cycles utilized, which affect your
11page load speeds. By caching your pages, since they are saved in their
12fully rendered state, you can achieve performance that nears that of
13static web pages.
14
15How Does Caching Work?
16======================
17
18Caching can be enabled on a per-page basis, and you can set the length
19of time that a page should remain cached before being refreshed. When a
20page is loaded for the first time, the cache file will be written to
21your application/cache folder. On subsequent page loads the cache file
22will be retrieved and sent to the requesting user's browser. If it has
23expired, it will be deleted and refreshed before being sent to the
24browser.
25
Andrey Andreev16a704c2012-11-09 17:25:00 +020026.. note: The Benchmark tag is not cached so you can still view your page
27 load speed when caching is enabled.
Derek Jones8ede1a22011-10-05 13:34:52 -050028
29Enabling Caching
30================
31
32To enable caching, put the following tag in any of your controller
Andrey Andreev16a704c2012-11-09 17:25:00 +020033methods::
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Andrey Andreev16a704c2012-11-09 17:25:00 +020035 $this->output->cache($n);
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreev16a704c2012-11-09 17:25:00 +020037Where ``$n`` is the number of **minutes** you wish the page to remain
38cached between refreshes.
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Andrey Andreev16a704c2012-11-09 17:25:00 +020040The above tag can go anywhere within a method. It is not affected by
Derek Jones8ede1a22011-10-05 13:34:52 -050041the order that it appears, so place it wherever it seems most logical to
42you. Once the tag is in place, your pages will begin being cached.
43
Derek Jonesaf8da302011-10-05 17:40:07 -050044.. important:: Because of the way CodeIgniter stores content for output,
Andrey Andreev16a704c2012-11-09 17:25:00 +020045 caching will only work if you are generating display for your
46 controller with a :doc:`view <./views>`.
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48.. note:: Before the cache files can be written you must set the file
Andrey Andreev16a704c2012-11-09 17:25:00 +020049 permissions on your *application/cache/* directory such that
50 it is writable.
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52Deleting Caches
53===============
54
55If you no longer wish to cache a file you can remove the caching tag and
Andrey Andreev16a704c2012-11-09 17:25:00 +020056it will no longer be refreshed when it expires.
57
58.. note:: Removing the tag will not delete the cache immediately. It will
59 have to expire normally. If you need to remove it earlier you
60 will need to manually delete it from your cache directory.