Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Web Page Caching |
| 3 | ################ |
| 4 | |
| 5 | CodeIgniter lets you cache your pages in order to achieve maximum |
| 6 | performance. |
| 7 | |
| 8 | Although CodeIgniter is quite fast, the amount of dynamic information |
| 9 | you display in your pages will correlate directly to the server |
| 10 | resources, memory, and processing cycles utilized, which affect your |
| 11 | page load speeds. By caching your pages, since they are saved in their |
| 12 | fully rendered state, you can achieve performance that nears that of |
| 13 | static web pages. |
| 14 | |
| 15 | How Does Caching Work? |
| 16 | ====================== |
| 17 | |
| 18 | Caching can be enabled on a per-page basis, and you can set the length |
| 19 | of time that a page should remain cached before being refreshed. When a |
| 20 | page is loaded for the first time, the cache file will be written to |
| 21 | your application/cache folder. On subsequent page loads the cache file |
| 22 | will be retrieved and sent to the requesting user's browser. If it has |
| 23 | expired, it will be deleted and refreshed before being sent to the |
| 24 | browser. |
| 25 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 26 | .. note: The Benchmark tag is not cached so you can still view your page |
| 27 | load speed when caching is enabled. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
| 29 | Enabling Caching |
| 30 | ================ |
| 31 | |
| 32 | To enable caching, put the following tag in any of your controller |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 33 | methods:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 35 | $this->output->cache($n); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 37 | Where ``$n`` is the number of **minutes** you wish the page to remain |
| 38 | cached between refreshes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 40 | The above tag can go anywhere within a method. It is not affected by |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | the order that it appears, so place it wherever it seems most logical to |
| 42 | you. Once the tag is in place, your pages will begin being cached. |
| 43 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 44 | .. important:: Because of the way CodeIgniter stores content for output, |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 45 | caching will only work if you are generating display for your |
| 46 | controller with a :doc:`view <./views>`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | 155ee72 | 2014-01-10 15:50:54 +0200 | [diff] [blame] | 48 | .. important:: If you change configuration options that might affect |
| 49 | your output, you have to manually delete your cache files. |
| 50 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | .. note:: Before the cache files can be written you must set the file |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 52 | permissions on your *application/cache/* directory such that |
| 53 | it is writable. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | Deleting Caches |
| 56 | =============== |
| 57 | |
| 58 | If you no longer wish to cache a file you can remove the caching tag and |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 59 | it will no longer be refreshed when it expires. |
| 60 | |
| 61 | .. note:: Removing the tag will not delete the cache immediately. It will |
Andrey Andreev | b37d2bc | 2012-11-30 02:19:35 +0200 | [diff] [blame] | 62 | have to expire normally. |
| 63 | |
| 64 | If you need to manually delete the cache, you can use the ``delete_cache()`` |
| 65 | method:: |
| 66 | |
| 67 | // Deletes cache for the currently requested URI |
| 68 | $this->output->delete_cache(); |
| 69 | |
| 70 | // Deletes cache for /foo/bar |
| 71 | $this->output->delete_cache('/foo/bar'); |