blob: fa67a52f84ee60c17ad7c9bf813a18153b6b1e6f [file] [log] [blame]
admin43a823e2006-09-27 01:57:10 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
admin43a823e2006-09-27 01:57:10 +00006
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
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
admin43a823e2006-09-27 01:57:10 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
admin43a823e2006-09-27 01:57:10 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
admin43a823e2006-09-27 01:57:10 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<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>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000036<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
admin43a823e2006-09-27 01:57:10 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
admin43a823e2006-09-27 01:57:10 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
51Database Caching Class
52</td>
Derek Allardbc030912007-06-24 18:25:29 +000053<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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
admin43a823e2006-09-27 01:57:10 +000054</tr>
55</table>
56<!-- END BREADCRUMB -->
57
58
59<br clear="all" />
60
61
62<!-- START CONTENT -->
63<div id="content">
64
65<h1>Database Caching Class</h1>
66
admin5294f4f2006-10-06 02:10:05 +000067<p>The Database Caching Class permits you to cache your queries as text files for reduced database load.</p>
admin43a823e2006-09-27 01:57:10 +000068
admin5ffba1e2006-10-05 04:23:18 +000069<p class="important"><strong>Important:</strong>&nbsp; This class is initialized automatically by the database driver
admin5294f4f2006-10-06 02:10:05 +000070when caching is enabled. Do NOT load this class manually.<br /><br />
admin43a823e2006-09-27 01:57:10 +000071
admin5294f4f2006-10-06 02:10:05 +000072<strong>Also note:</strong>&nbsp; Not all query result functions are available when you use caching. Please read this page carefully.</p>
admin43a823e2006-09-27 01:57:10 +000073
admin11c06c72006-10-05 05:12:27 +000074<h2>Enabling Caching</h2>
75
admin5294f4f2006-10-06 02:10:05 +000076<p>Caching is enabled in three steps:</p>
admin11c06c72006-10-05 05:12:27 +000077
78<ul>
admin5294f4f2006-10-06 02:10:05 +000079<li>Create a writable directory on your server where the cache files can be stored.</li>
80<li>Set the path to your cache folder in your <dfn>application/config/database.php</dfn> file.</li>
81<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>
admin11c06c72006-10-05 05:12:27 +000082</ul>
admin43a823e2006-09-27 01:57:10 +000083
adminadb899a2006-10-10 23:11:33 +000084<p>Once enabled, caching will happen automatically whenever a page is loaded that contains database queries.</p>
admin0a35e3d2006-10-06 06:05:02 +000085
admin43a823e2006-09-27 01:57:10 +000086
admin5294f4f2006-10-06 02:10:05 +000087<h2>How Does Caching Work?</h2>
adminf3428b52006-10-05 05:19:15 +000088
Derek Allardd2df9bc2007-04-15 17:41:17 +000089<p>CodeIgniter's query caching system happens dynamically when your pages are viewed.
admin0a35e3d2006-10-06 06:05:02 +000090When caching is enabled, the first time a web page is loaded, the query result object will
admin5294f4f2006-10-06 02:10:05 +000091be 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
92accessing your database. Your database usage can effectively be reduced to zero for any pages that have been cached.</p>
adminf3428b52006-10-05 05:19:15 +000093
admine334c472006-10-21 19:44:22 +000094<p>Only <dfn>read-type</dfn> (SELECT) queries can be cached, since these are the only type of queries that produce a result.
admin5294f4f2006-10-06 02:10:05 +000095<dfn>Write-type</dfn> (INSERT, UPDATE, etc.) queries, since they don't generate a result, will not be cached by the system.</p>
adminf3428b52006-10-05 05:19:15 +000096
admine334c472006-10-21 19:44:22 +000097<p>Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them. The caching system
98permits you clear caches associated with individual pages, or you can delete the entire collection of cache files.
admin0a35e3d2006-10-06 06:05:02 +000099Typically you'll to use the housekeeping functions described below to delete cache files after certain
100events take place, like when you've added new information to your database.</p>
adminf3428b52006-10-05 05:19:15 +0000101
admin5294f4f2006-10-06 02:10:05 +0000102<h2>Will Caching Improve Your Site's Performance?</h2>
103
admine334c472006-10-21 19:44:22 +0000104<p>Getting a performance gain as a result of caching depends on many factors.
admin0a35e3d2006-10-06 06:05:02 +0000105If you have a highly optimized database under very little load, you probably won't see a performance boost.
admine334c472006-10-21 19:44:22 +0000106If your database is under heavy use you probably will see an improved response, assuming your file-system is not
admin0a35e3d2006-10-06 06:05:02 +0000107overly taxed. Remember that caching simply changes how your information is retrieved, shifting it from being a database
adminadb899a2006-10-10 23:11:33 +0000108operation to a file-system one.</p>
admin5294f4f2006-10-06 02:10:05 +0000109
adminadb899a2006-10-10 23:11:33 +0000110<p>In some clustered server environments, for example, caching may be detrimental since file-system operations are so intense.
admin0a35e3d2006-10-06 06:05:02 +0000111On single servers in shared environments, caching will probably be beneficial. Unfortunately there is no
admin5294f4f2006-10-06 02:10:05 +0000112single answer to the question of whether you should cache your database. It really depends on your situation.</p>
113
114<h2>How are Cache Files Stored?</h2>
115
Derek Allardd2df9bc2007-04-15 17:41:17 +0000116<p>CodeIgniter places the result of EACH query into its own cache file. Sets of cache files are further organized into
admin0a35e3d2006-10-06 06:05:02 +0000117sub-folders corresponding to your controller functions. To be precise, the sub-folders are named identically to the
118first two segments of your URI (the controller class name and function name).</p>
admin5294f4f2006-10-06 02:10:05 +0000119
120<p>For example, let's say you have a controller called <dfn>blog</dfn> with a function called <dfn>comments</dfn> that
admine334c472006-10-21 19:44:22 +0000121contains three queries. The caching system will create a cache folder
adminadb899a2006-10-10 23:11:33 +0000122called <kbd>blog+comments</kbd>, into which it will write three cache files.</p>
admin5294f4f2006-10-06 02:10:05 +0000123
admin0a35e3d2006-10-06 06:05:02 +0000124<p>If you use dynamic queries that change based on information in your URI (when using pagination, for example), each instance of
admine334c472006-10-21 19:44:22 +0000125the query will produce its own cache file. It's possible, therefore, to end up with many times more cache files than you have
admin5294f4f2006-10-06 02:10:05 +0000126queries.</p>
adminf3428b52006-10-05 05:19:15 +0000127
128
admin5294f4f2006-10-06 02:10:05 +0000129<h2>Managing your Cache Files</h2>
130
131<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
admine334c472006-10-21 19:44:22 +0000132that allows user commenting. Whenever a new comment is submitted you'll want to delete the cache files associated with the
133controller function that serves up your comments. You'll find two delete functions described below that help you
admin5294f4f2006-10-06 02:10:05 +0000134clear data.</p>
adminf3428b52006-10-05 05:19:15 +0000135
136
admin0a35e3d2006-10-06 06:05:02 +0000137<h2>Not All Database Functions Work with Caching</h2>
138
139<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,
140some of the query result functions are not available for use.</p>
141
142<p>The following functions <kbd>ARE NOT</kbd> available when using a cached result object:</p>
143
144<ul>
145<li>num_fields()</li>
146<li>field_names()</li>
147<li>field_data()</li>
148<li>free_result()</li>
149</ul>
150
151<p>Also, the two database resources (result_id and conn_id) are not available when caching, since result resources only
152pertain to run-time operations.</p>
153
154
155<br />
156
admin5294f4f2006-10-06 02:10:05 +0000157<h1>Function Reference</h1>
adminf3428b52006-10-05 05:19:15 +0000158
159
160
adminadb899a2006-10-10 23:11:33 +0000161<h2>$this->db->cache_on()&nbsp; / &nbsp; $this->db->cache_off()</h2>
162
163<p>Manually enables/disables caching. This can be useful if you want to
164keep certain queries from being cached. Example:</p>
165
166<code>
167// Turn caching on<br />
168$this->db->cache_on();<br />
169$query = $this->db->query("SELECT * FROM mytable");<br />
170<br />
171// Turn caching off for this one query<br />
172$this->db->cache_off();<br />
173$query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");<br />
174<br />
175// Turn caching back on<br />
176$this->db->cache_on();<br />
177$query = $this->db->query("SELECT * FROM another_table");
178</code>
adminf3428b52006-10-05 05:19:15 +0000179
180
adminadb899a2006-10-10 23:11:33 +0000181<h2>$this->db->cache_delete()</h2>
182
183<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>
184
185<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
186a 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
187called <dfn>blog+comments</dfn>. To delete those particular cache files you will use:</p>
188
189<code>$this->db->cache_delete('blog', 'comments');</code>
190
191<p>If you do not use any parameters the current URI will be used when determining what should be cleared.</p>
192
193
194<h2>$this->db->cache_delete_all()</h2>
195
196<p>Clears all existing cache files. Example:</p>
197
198<code>$this->db->cache_delete_all();</code>
adminf3428b52006-10-05 05:19:15 +0000199
200
201
202
203
204
205
206
admin43a823e2006-09-27 01:57:10 +0000207
208
209</div>
210<!-- END CONTENT -->
211
212
213<div id="footer">
214<p>
215Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
216&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
217<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
218<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Rick Ellis325197e2006-11-20 17:29:05 +0000219Next Topic:&nbsp;&nbsp;<a href="utilities.html">Database Utilities Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000220</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000221<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
admin43a823e2006-09-27 01:57:10 +0000222</div>
223
224</body>
225</html>