admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 5 | <title>Database Caching Class : CodeIgniter User Guide</title>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 21 |
|
| 22 | </head>
|
| 23 | <body>
|
| 24 |
|
| 25 | <!-- START NAVIGATION -->
|
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Derek Allard | 197d10b | 2008-02-12 04:20:38 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 33 | </tr>
|
| 34 | </table>
|
| 35 | </div>
|
| 36 | <!-- END NAVIGATION -->
|
| 37 |
|
| 38 |
|
| 39 | <!-- START BREADCRUMB -->
|
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 41 | <tr>
|
| 42 | <td id="breadcrumb">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | <a href="index.html">Database Library</a> ›
|
| 46 | Database Caching Class
|
| 47 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 48 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 49 | </tr>
|
| 50 | </table>
|
| 51 | <!-- END BREADCRUMB -->
|
| 52 |
|
| 53 |
|
| 54 | <br clear="all" />
|
| 55 |
|
| 56 |
|
| 57 | <!-- START CONTENT -->
|
| 58 | <div id="content">
|
| 59 |
|
| 60 | <h1>Database Caching Class</h1>
|
| 61 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 62 | <p>The Database Caching Class permits you to cache your queries as text files for reduced database load.</p>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 63 |
|
admin | 5ffba1e | 2006-10-05 04:23:18 +0000 | [diff] [blame] | 64 | <p class="important"><strong>Important:</strong> This class is initialized automatically by the database driver
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 65 | when caching is enabled. Do NOT load this class manually.<br /><br />
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 66 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 67 | <strong>Also note:</strong> Not all query result functions are available when you use caching. Please read this page carefully.</p>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 68 |
|
admin | 11c06c7 | 2006-10-05 05:12:27 +0000 | [diff] [blame] | 69 | <h2>Enabling Caching</h2>
|
| 70 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 71 | <p>Caching is enabled in three steps:</p>
|
admin | 11c06c7 | 2006-10-05 05:12:27 +0000 | [diff] [blame] | 72 |
|
| 73 | <ul>
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 74 | <li>Create a writable directory on your server where the cache files can be stored.</li>
|
| 75 | <li>Set the path to your cache folder in your <dfn>application/config/database.php</dfn> file.</li>
|
| 76 | <li>Enable the caching feature, either globally by setting the preference in your <dfn>application/config/database.php</dfn> file, or manually as described below.</li>
|
admin | 11c06c7 | 2006-10-05 05:12:27 +0000 | [diff] [blame] | 77 | </ul>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 78 |
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 79 | <p>Once enabled, caching will happen automatically whenever a page is loaded that contains database queries.</p>
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 80 |
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 81 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 82 | <h2>How Does Caching Work?</h2>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 83 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 84 | <p>CodeIgniter's query caching system happens dynamically when your pages are viewed.
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 85 | When caching is enabled, the first time a web page is loaded, the query result object will
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 86 | be serialized and stored in a text file on your server. The next time the page is loaded the cache file will be used instead of
|
| 87 | accessing your database. Your database usage can effectively be reduced to zero for any pages that have been cached.</p>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 88 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 89 | <p>Only <dfn>read-type</dfn> (SELECT) queries can be cached, since these are the only type of queries that produce a result.
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 90 | <dfn>Write-type</dfn> (INSERT, UPDATE, etc.) queries, since they don't generate a result, will not be cached by the system.</p>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 91 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 92 | <p>Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them. The caching system
|
| 93 | permits you clear caches associated with individual pages, or you can delete the entire collection of cache files.
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 94 | Typically you'll to use the housekeeping functions described below to delete cache files after certain
|
| 95 | events take place, like when you've added new information to your database.</p>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 96 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 97 | <h2>Will Caching Improve Your Site's Performance?</h2>
|
| 98 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 99 | <p>Getting a performance gain as a result of caching depends on many factors.
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 100 | If you have a highly optimized database under very little load, you probably won't see a performance boost.
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 101 | If your database is under heavy use you probably will see an improved response, assuming your file-system is not
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 102 | overly taxed. Remember that caching simply changes how your information is retrieved, shifting it from being a database
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 103 | operation to a file-system one.</p>
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 104 |
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 105 | <p>In some clustered server environments, for example, caching may be detrimental since file-system operations are so intense.
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 106 | On single servers in shared environments, caching will probably be beneficial. Unfortunately there is no
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 107 | single answer to the question of whether you should cache your database. It really depends on your situation.</p>
|
| 108 |
|
| 109 | <h2>How are Cache Files Stored?</h2>
|
| 110 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 111 | <p>CodeIgniter places the result of EACH query into its own cache file. Sets of cache files are further organized into
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 112 | sub-folders corresponding to your controller functions. To be precise, the sub-folders are named identically to the
|
| 113 | first two segments of your URI (the controller class name and function name).</p>
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 114 |
|
| 115 | <p>For example, let's say you have a controller called <dfn>blog</dfn> with a function called <dfn>comments</dfn> that
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 116 | contains three queries. The caching system will create a cache folder
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 117 | called <kbd>blog+comments</kbd>, into which it will write three cache files.</p>
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 118 |
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 119 | <p>If you use dynamic queries that change based on information in your URI (when using pagination, for example), each instance of
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 120 | the query will produce its own cache file. It's possible, therefore, to end up with many times more cache files than you have
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 121 | queries.</p>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 122 |
|
| 123 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 124 | <h2>Managing your Cache Files</h2>
|
| 125 |
|
| 126 | <p>Since cache files do not expire, you'll need to build deletion routines into your application. For example, let's say you have a blog
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 127 | that allows user commenting. Whenever a new comment is submitted you'll want to delete the cache files associated with the
|
| 128 | controller function that serves up your comments. You'll find two delete functions described below that help you
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 129 | clear data.</p>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 130 |
|
| 131 |
|
admin | 0a35e3d | 2006-10-06 06:05:02 +0000 | [diff] [blame] | 132 | <h2>Not All Database Functions Work with Caching</h2>
|
| 133 |
|
| 134 | <p>Lastly, we need to point out that the result object that is cached is a simplified version of the full result object. For that reason,
|
| 135 | some of the query result functions are not available for use.</p>
|
| 136 |
|
| 137 | <p>The following functions <kbd>ARE NOT</kbd> available when using a cached result object:</p>
|
| 138 |
|
| 139 | <ul>
|
| 140 | <li>num_fields()</li>
|
| 141 | <li>field_names()</li>
|
| 142 | <li>field_data()</li>
|
| 143 | <li>free_result()</li>
|
| 144 | </ul>
|
| 145 |
|
| 146 | <p>Also, the two database resources (result_id and conn_id) are not available when caching, since result resources only
|
| 147 | pertain to run-time operations.</p>
|
| 148 |
|
| 149 |
|
| 150 | <br />
|
| 151 |
|
admin | 5294f4f | 2006-10-06 02:10:05 +0000 | [diff] [blame] | 152 | <h1>Function Reference</h1>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 153 |
|
| 154 |
|
| 155 |
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 156 | <h2>$this->db->cache_on() / $this->db->cache_off()</h2>
|
| 157 |
|
| 158 | <p>Manually enables/disables caching. This can be useful if you want to
|
| 159 | keep certain queries from being cached. Example:</p>
|
| 160 |
|
| 161 | <code>
|
| 162 | // Turn caching on<br />
|
| 163 | $this->db->cache_on();<br />
|
| 164 | $query = $this->db->query("SELECT * FROM mytable");<br />
|
| 165 | <br />
|
| 166 | // Turn caching off for this one query<br />
|
| 167 | $this->db->cache_off();<br />
|
| 168 | $query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");<br />
|
| 169 | <br />
|
| 170 | // Turn caching back on<br />
|
| 171 | $this->db->cache_on();<br />
|
| 172 | $query = $this->db->query("SELECT * FROM another_table");
|
| 173 | </code>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 174 |
|
| 175 |
|
admin | adb899a | 2006-10-10 23:11:33 +0000 | [diff] [blame] | 176 | <h2>$this->db->cache_delete()</h2>
|
| 177 |
|
| 178 | <p>Deletes the cache files associated with a particular page. This is useful if you need to clear caching after you update your database.</p>
|
| 179 |
|
| 180 | <p>The caching system saves your cache files to folders that correspond to the URI of the page you are viewing. For example, if you are viewing
|
| 181 | a page at <dfn>www.your-site.com/index.php/blog/comments</dfn>, the caching system will put all cache files associated with it in a folder
|
| 182 | called <dfn>blog+comments</dfn>. To delete those particular cache files you will use:</p>
|
| 183 |
|
| 184 | <code>$this->db->cache_delete('blog', 'comments');</code>
|
| 185 |
|
| 186 | <p>If you do not use any parameters the current URI will be used when determining what should be cleared.</p>
|
| 187 |
|
| 188 |
|
| 189 | <h2>$this->db->cache_delete_all()</h2>
|
| 190 |
|
| 191 | <p>Clears all existing cache files. Example:</p>
|
| 192 |
|
| 193 | <code>$this->db->cache_delete_all();</code>
|
admin | f3428b5 | 2006-10-05 05:19:15 +0000 | [diff] [blame] | 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 202 |
|
| 203 |
|
| 204 | </div>
|
| 205 | <!-- END CONTENT -->
|
| 206 |
|
| 207 |
|
| 208 | <div id="footer">
|
| 209 | <p>
|
| 210 | Previous Topic: <a href="call_function.html">Custom Function Calls</a>
|
| 211 | ·
|
| 212 | <a href="#top">Top of Page</a> ·
|
| 213 | <a href="../index.html">User Guide Home</a> ·
|
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 214 | Next Topic: <a href="utilities.html">Database Utilities Class</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 215 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 216 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | 43a823e | 2006-09-27 01:57:10 +0000 | [diff] [blame] | 217 | </div>
|
| 218 |
|
| 219 | </body>
|
| 220 | </html> |