1 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
3 <title>CarbonPHP Documentation
</title>
4 <style type=
"text/css">
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
33 margin: 10px 0 10px 0;
34 list-style-position: inside
;
42 border: 1px dotted
#444;
50 border: 1px solid
#ccc;
61 <h1>Database Caching
</h1>
63 <p>The database class allows you to cache your queries as text files for reducing the database load.
</p>
65 <p>The caching class is loaded automatically loaded by the database class you do not have to manually load
70 <h1>Enabling Caching
</h1>
72 <p>Caching is enabled by following these three steps.
</p>
75 <li>Create a directory on the server where the cache files will be stored.
</li>
76 <li>Set the path to your cache directory in your
<b>application/config/database.php
</b> file.
</li>
77 <li>Enabled the caching feature, either globally in the configuration settings or manually as described
81 <p>Once caching is enabled caching will happen automatically whenever a page is loaded that contains any
86 <h1>How Does Caching Work
</h1>
88 <p>CarbonPHP caching happens dynamically when your pages are viewed. When caching is enabled the first time
89 a page is loaded the query result object will be serialised and stored in a text file on the server. The next
90 time the page is loaded the cache file will be served instead of accessing the database.
</p>
92 <p>Only 'read' type queries can be cached, since these are the only type of queries that produce a result
95 <p>Cache files do not expire. Any queries that have been called will remain cached until you delete them.
96 The caching system permits you to clear caches associated with individual pages, or you can delete the
97 whole set of cache files.
</p>
100 <div class=
"content">
101 <h1>Does Caching Improve Site Performance
</h1>
103 <p>Getting a performance gain as a result of caching depends on many factors. If you have a highly optimised
104 database under very little load you will probably not noticed an improved performance. If your database
105 is under heavy load you probably will see an improved response.
</p>
107 <p>In some clustered server environments for example caching may be detrimental since file system operations
108 are so instense. On single servers in shared environments caching you probably be more benficial.
</p>
111 <div class=
"content">
112 <h1>How are Cache Files Stored
</h1>
114 <p>CarbonPHP places the result of each query into it's own cache file which are then organised into
115 subdirectories corresponding to the name of your controller and method.
</p>
117 <p>For example if you have a controller called
<b>blog
</b> and a method called
<b>post
</b> that contains
118 three queries, the caching system will create a cache directory called
<b>blog+post
</b> into which it
119 will create three cache files, one for each query.
</p>
122 <div class=
"content">
123 <h1>Managing Cache Files
</h1>
125 <p>Since cache files do not expire you will need to use the built in deletion methods to remove old cache
129 <div class=
"content">
130 <h1>Unsupported Methods
</h1>
132 <p>Some methods are not supported by the caching system.
</p>
135 <li>num_fields()
</li>
136 <li>field_names()
</li>
137 <li>field_data()
</li>
138 <li>free_result()
</li>
141 <p>The database connection ID and result ID are not available when caching.
</p>
144 <div class=
"content">
145 <h1>$this-
>db-
>cache_on() and $this-
>db-
>cache_off()
</h1>
147 <p>This method enables and disabled the caching manually. This can be useful if you wish to keep certain
148 queries from being cached.
</p>
150 <div class=
"example">
151 $this-
>db-
>cache_on();
<br />
152 $query = $this-
>db-
>query('SELECT * FROM `some_table`');
<br />
154 $this-
>db-
>cache_off();
<br />
155 $query = $this-
>db-
>query('SELECT * FROM `another_table`');
<br />
157 $this-
>db-
>cache_on();
<br />
158 $query = $this-
>db-
>query('SELECT * FROM `more_table`');
162 <div class=
"content">
163 <h1>$this-
>db-
>cache_delete()
</h1>
165 <p>This method deletes the cache files associated with a particular page. This is useful if you need to clear
166 a cache file after you update your database.
</p>
168 <p>The caching system saves your cache files to directories correspond to the URI of the page you are viewing.
169 To delete the cache files of a controller and method you should the following.
</p>
171 <div class=
"example">
172 $this-
>db-
>cache_delete('blog', 'posts');
175 <p>If you do not pass any parameters to the function it will determine the cache files based on the current
179 <div class=
"content">
180 <h1>$this-
>db-
>cache_delete_all()
</h1>
182 <p>Clears all the existing cache files.
</p>
184 <div class=
"example">
185 $this-
>db-
>cache_delete_all();