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