blob: 0f852cf03390e0c4f5edb9aac9e449937e0381bd [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
5<title>Code Igniter User Guide</title>
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
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() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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>
36<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
37<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<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>
53<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
54</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
67<p>The Database Caching Class contains functions that permit you to cache your queries.</p>
68
admin5ffba1e2006-10-05 04:23:18 +000069<p class="important"><strong>Important:</strong>&nbsp; This class is initialized automatically by the database driver
admin11c06c72006-10-05 05:12:27 +000070when caching is enabled. Do NOT load this class manually.
admin43a823e2006-09-27 01:57:10 +000071More info below...</p>
72
admin5ffba1e2006-10-05 04:23:18 +000073<h2>How Does Caching Work?</h2>
admin43a823e2006-09-27 01:57:10 +000074
admin11c06c72006-10-05 05:12:27 +000075<p>When caching is enabled, anytime a "read" type query (SELECT) is run, the result object will
76be serialized and stored in a text file on your server. Subsequent calls to that query will use the result from the cache file
77rather then accessing your database. In other words, the first time a page is loaded a cache file will be written.
78The next time the page is loaded the cached file will be used.</p>
admin43a823e2006-09-27 01:57:10 +000079
admin11c06c72006-10-05 05:12:27 +000080<p>When a "write" type query (INSERT, UPDATE, etc.) is run , any cache files associated with the particular page being viewed
adminf3428b52006-10-05 05:19:15 +000081will be deleted automatically. If you need to update some data with every page load (user stats, for example) you will
82need to manually disable caching just before running your "write" query, then re-enable it just
83after. Otherwise, your site will be caught in a cycle of writing/deleting caches with every page view, creating more load then if you were not using
admin11c06c72006-10-05 05:12:27 +000084caching. More information on this will be found below.</p>
admin43a823e2006-09-27 01:57:10 +000085
admin43a823e2006-09-27 01:57:10 +000086
admin11c06c72006-10-05 05:12:27 +000087<p>Although caching will reduce your database load, dealing with cache files does generate more
88up-front processing and file-system operations, as cache files are created and read. Instead of accessing your database for information
89text files are used.</p>
admin43a823e2006-09-27 01:57:10 +000090
admin11c06c72006-10-05 05:12:27 +000091<p>Whether you see a performance gain as a result of caching is dependant on many factors.
admin5ffba1e2006-10-05 04:23:18 +000092For example, if you have a highly optimized database under very little load, you probably won't see a performance boost.
93If your database is under heavy use you probably will see an improved response, assuming your filesystem is not
admin11c06c72006-10-05 05:12:27 +000094overly taxed. In some clustered server environments caching may be detrimental since filesystem operations are so intense.
95On single servers (particularly in shared enironments) caching will probably be beneficial. Unfortunately there is no
96single answer to the question of whether you should cache your database. It really depends on your situation.</p>
admin43a823e2006-09-27 01:57:10 +000097
admin11c06c72006-10-05 05:12:27 +000098<h2>Enabling Caching</h2>
99
100<p>Enabling caching requires three steps:</p>
101
102<ul>
103<li>Creating a directory on your server where the cache files will be written.</li>
104<li>Setting the path to your cache folder in your <dfn>application/config/database.php</dfn> file.</li>
105<li>Enalbling the caching preference either in your database config file or manually in your controllers.</li>
106</ul>
admin43a823e2006-09-27 01:57:10 +0000107
108
adminf3428b52006-10-05 05:19:15 +0000109<h2>Caching Example</h2>
110
111<p>Here is an example showing how you can selectively cache some queries and not others. Notice that the "write" type queries
112are not.
113
114<code>
115<kbd>$this->db->cache_on();</kbd> // Turns on caching. We assume that you've set up a valid cache foler...
116
117$query = $this->db->query("SELECT * FROM blog LIMIT 10);
118
119foreach ($query->result() as $row)
120{
121 echo '<h3>'.$row->title.'</h3>';
122 echo '<p>'>.$row->content.'</p>';
123}
124
125
126
127
128
129
130
131
132
133</code>
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
admin43a823e2006-09-27 01:57:10 +0000151
152
153</div>
154<!-- END CONTENT -->
155
156
157<div id="footer">
158<p>
159Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
160&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
161<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
162<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
163Next Topic:&nbsp;&nbsp;<a href="export.html">Database Export Class</a>
164<p>
165<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
166</div>
167
168</body>
169</html>