summaryrefslogtreecommitdiffstats
path: root/user_guide/database/caching.html
blob: 8bf5343527f8f879708adf6a15fa0fd7aba0b9bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<title>Code Igniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript">
window.onload = function() {
	myHeight = new fx.Height('nav', {duration: 400}); 
	myHeight.hide();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='Rick Ellis' />
<meta name='description' content='Code Igniter User Guide' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<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>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
Database Caching Class
</td>
<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>
</tr>
</table>
<!-- END BREADCRUMB -->


<br clear="all" />


<!-- START CONTENT -->
<div id="content">

<h1>Database Caching Class</h1>

<p>The Database Caching Class permits you to cache your queries as text files for reduced database load.</p>

<p class="important"><strong>Important:</strong>&nbsp; This class is initialized automatically by the database driver
when caching is enabled.  Do NOT load this class manually.<br /><br />

<strong>Also note:</strong>&nbsp; Not all query result functions are available when you use caching. Please read this page carefully.</p>

<h2>Enabling Caching</h2>

<p>Caching is enabled in three steps:</p>

<ul>
<li>Create a writable directory on your server where the cache files can be stored.</li>
<li>Set the path to your cache folder in your <dfn>application/config/database.php</dfn> file.</li>
<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>
</ul>


<h2>How Does Caching Work?</h2>

<p>Code Igniter's query caching system happens dynamically when your pages are viewed. 
When caching is enabled, the first time a webpage is loaded, the query result object will
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
accessing your database.  Your database usage can effectively be reduced to zero for any pages that have been cached.</p>

<p>Only <dfn>read-type</dfn> (SELECT) queries can be cached, since these are the only type of queries that produce a result.  
<dfn>Write-type</dfn> (INSERT, UPDATE, etc.) queries, since they don't generate a result, will not be cached by the system.</p>

<p>Cache files do NOT expire.  Any queries that have been cached will remain cached until you delete them.  The caching system does
have an "auto-delete"feature, as described below.  It also lets you manually clear caches associated with individulal pages, or
you can delete the entire collection of cache files.</p>

<h2>Will Caching Improve Your Site's Performance?</h2>

<p>Maybe. Whether you see a performance gain as a result of caching depends on many factors. 
For example, if you have a highly optimized database under very little load, you probably won't see a performance boost.
If your database is under heavy use you probably will see an improved response, assuming your filesystem is not 
overly taxed. Remember that caching simply changes how your information is retrieved, shifting it from being a database
operation to a filesystem one.</p>

<p>In some clustered server environments caching may be detrimental since filesystem operations are so intense.
On single servers (particularly in shared enironments) caching will probably be beneficial. Unfortunately there is no
single answer to the question of whether you should cache your database.  It really depends on your situation.</p>

<h2>How are Cache Files Stored?</h2>

<p>Code Igniter places the result of EACH query into its own cache file.  Sets of cache files are further organized into 
sub-folders corrsponding to your controller functions.</p>

<p>For example, let's say you have a controller called <dfn>blog</dfn> with a function called <dfn>comments</dfn> that
contains three queries.  The caching system will create a cache folder 
called <kbd>blog_comments</kbd>, into which it will write three cache files.</p>

<p>If your have dynamic queries that change based on inormation in your URI (when using pagination, for example), each instance of
the query will produce its own cache file.  It's possible, therefore, to end up with many times more cache files than you have 
queries.</p>


<h2>Managing your Cache Files</h2>

<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
that allows user commenting.  Whenever a new comment is submitted you'll want to delete the cache files associated with the 
controller function that serves up your comments.  You'll find two delete functions described below that help you 
clear data.</p>


<h1>Function Reference</h1>















</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="call_function.html">Custom Function Calls</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="export.html">Database Export Class</a>
<p>
<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>
</div>

</body>
</html>