blob: d73120a93eb0987f974e312cb97b2346ba9c929f [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Database Caching Class
3######################
4
5The Database Caching Class permits you to cache your queries as text
6files for reduced database load.
7
8.. important:: This class is initialized automatically by the database
9 driver when caching is enabled. Do NOT load this class manually.
10
11.. important:: Not all query result functions are available when you
12 use caching. Please read this page carefully.
13
14Enabling Caching
15================
16
17Caching is enabled in three steps:
18
19- Create a writable directory on your server where the cache files can
20 be stored.
21- Set the path to your cache folder in your
22 application/config/database.php file.
23- Enable the caching feature, either globally by setting the preference
24 in your application/config/database.php file, or manually as
25 described below.
26
27Once enabled, caching will happen automatically whenever a page is
28loaded that contains database queries.
29
30How Does Caching Work?
31======================
32
33CodeIgniter's query caching system happens dynamically when your pages
34are viewed. When caching is enabled, the first time a web page is
35loaded, the query result object will be serialized and stored in a text
36file on your server. The next time the page is loaded the cache file
37will be used instead of accessing your database. Your database usage can
38effectively be reduced to zero for any pages that have been cached.
39
40Only read-type (SELECT) queries can be cached, since these are the only
41type of queries that produce a result. Write-type (INSERT, UPDATE, etc.)
42queries, since they don't generate a result, will not be cached by the
43system.
44
45Cache files DO NOT expire. Any queries that have been cached will remain
46cached until you delete them. The caching system permits you clear
47caches associated with individual pages, or you can delete the entire
48collection of cache files. Typically you'll want to use the housekeeping
49functions described below to delete cache files after certain events
50take place, like when you've added new information to your database.
51
52Will Caching Improve Your Site's Performance?
53=============================================
54
55Getting a performance gain as a result of caching depends on many
56factors. If you have a highly optimized database under very little load,
57you probably won't see a performance boost. If your database is under
58heavy use you probably will see an improved response, assuming your
59file-system is not overly taxed. Remember that caching simply changes
60how your information is retrieved, shifting it from being a database
61operation to a file-system one.
62
63In some clustered server environments, for example, caching may be
64detrimental since file-system operations are so intense. On single
65servers in shared environments, caching will probably be beneficial.
66Unfortunately there is no single answer to the question of whether you
67should cache your database. It really depends on your situation.
68
69How are Cache Files Stored?
70===========================
71
72CodeIgniter places the result of EACH query into its own cache file.
73Sets of cache files are further organized into sub-folders corresponding
74to your controller functions. To be precise, the sub-folders are named
75identically to the first two segments of your URI (the controller class
76name and function name).
77
78For example, let's say you have a controller called blog with a function
79called comments that contains three queries. The caching system will
80create a cache folder called blog+comments, into which it will write
81three cache files.
82
83If you use dynamic queries that change based on information in your URI
84(when using pagination, for example), each instance of the query will
85produce its own cache file. It's possible, therefore, to end up with
86many times more cache files than you have queries.
87
88Managing your Cache Files
89=========================
90
91Since cache files do not expire, you'll need to build deletion routines
92into your application. For example, let's say you have a blog that
93allows user commenting. Whenever a new comment is submitted you'll want
94to delete the cache files associated with the controller function that
95serves up your comments. You'll find two delete functions described
96below that help you clear data.
97
98Not All Database Functions Work with Caching
99============================================
100
101Lastly, we need to point out that the result object that is cached is a
102simplified version of the full result object. For that reason, some of
103the query result functions are not available for use.
104
105The following functions ARE NOT available when using a cached result
106object:
107
108- num_fields()
109- field_names()
110- field_data()
111- free_result()
112
113Also, the two database resources (result_id and conn_id) are not
114available when caching, since result resources only pertain to run-time
115operations.
116
117******************
118Function Reference
119******************
120
121$this->db->cache_on() / $this->db->cache_off()
122================================================
123
124Manually enables/disables caching. This can be useful if you want to
125keep certain queries from being cached. Example::
126
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400127 // Turn caching on
128 $this->db->cache_on();
129 $query = $this->db->query("SELECT * FROM mytable");
130
131 // Turn caching off for this one query
132 $this->db->cache_off();
133 $query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");
134
135 // Turn caching back on
136 $this->db->cache_on();
137 $query = $this->db->query("SELECT * FROM another_table");
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
139$this->db->cache_delete()
140==========================
141
142Deletes the cache files associated with a particular page. This is
143useful if you need to clear caching after you update your database.
144
145The caching system saves your cache files to folders that correspond to
146the URI of the page you are viewing. For example, if you are viewing a
147page at example.com/index.php/blog/comments, the caching system will put
148all cache files associated with it in a folder called blog+comments. To
149delete those particular cache files you will use::
150
151 $this->db->cache_delete('blog', 'comments');
152
153If you do not use any parameters the current URI will be used when
154determining what should be cleared.
155
156$this->db->cache_delete_all()
157===============================
158
159Clears all existing cache files. Example::
160
161 $this->db->cache_delete_all();
162