Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############## |
| 2 | Caching Driver |
| 3 | ############## |
| 4 | |
| 5 | CodeIgniter features wrappers around some of the most popular forms of |
| 6 | fast and dynamic caching. All but file-based caching require specific |
| 7 | server requirements, and a Fatal Exception will be thrown if server |
| 8 | requirements are not met. |
| 9 | |
| 10 | .. contents:: Table of Contents |
| 11 | |
| 12 | ************* |
| 13 | Example Usage |
| 14 | ************* |
| 15 | |
| 16 | The following example will load the cache driver, specify `APC <#apc>`_ |
| 17 | as the driver to use, and fall back to file-based caching if APC is not |
| 18 | available in the hosting environment. |
| 19 | |
| 20 | :: |
| 21 | |
| 22 | $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file')); |
| 23 | |
| 24 | if ( ! $foo = $this->cache->get('foo')) |
| 25 | { |
| 26 | echo 'Saving to the cache!<br />'; |
| 27 | $foo = 'foobarbaz!'; |
| 28 | |
| 29 | // Save into the cache for 5 minutes |
| 30 | $this->cache->save('foo', $foo, 300); |
| 31 | } |
| 32 | |
| 33 | echo $foo; |
| 34 | |
| 35 | ****************** |
| 36 | Function Reference |
| 37 | ****************** |
| 38 | |
| 39 | .. php:class:: CI_Cache |
| 40 | |
| 41 | is_supported() |
| 42 | =============== |
| 43 | |
| 44 | .. php:method:: is_supported ( $driver ) |
| 45 | |
| 46 | This function is automatically called when accessing drivers via |
| 47 | $this->cache->get(). However, if the individual drivers are used, make |
| 48 | sure to call this function to ensure the driver is supported in the |
| 49 | hosting environment. |
| 50 | |
| 51 | :param string $driver: the name of the caching driver |
| 52 | :returns: TRUE if supported, FALSE if not |
| 53 | :rtype: Boolean |
| 54 | |
| 55 | :: |
| 56 | |
| 57 | if ($this->cache->apc->is_supported() |
| 58 | { |
| 59 | if ($data = $this->cache->apc->get('my_cache')) |
| 60 | { |
| 61 | // do things. |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | get() |
| 67 | ===== |
| 68 | |
| 69 | .. php:method:: get ( $id ) |
| 70 | |
| 71 | This function will attempt to fetch an item from the cache store. If the |
| 72 | item does not exist, the function will return FALSE. |
| 73 | |
| 74 | :param string $id: name of cached item |
| 75 | :returns: The item if it exists, FALSE if it does not |
| 76 | :rtype: Mixed |
| 77 | |
| 78 | :: |
| 79 | |
| 80 | $foo = $this->cache->get('my_cached_item'); |
| 81 | |
| 82 | |
| 83 | save() |
| 84 | ====== |
| 85 | |
| 86 | .. php:method:: save ( $id , $data [, $ttl]) |
| 87 | |
| 88 | This function will save an item to the cache store. If saving fails, the |
| 89 | function will return FALSE. |
| 90 | |
| 91 | :param string $id: name of the cached item |
| 92 | :param mixed $data: the data to save |
| 93 | :param int $ttl: Time To Live, in seconds (default 60) |
| 94 | :returns: TRUE on success, FALSE on failure |
| 95 | :rtype: Boolean |
| 96 | |
| 97 | :: |
| 98 | |
| 99 | $this->cache->save('cache_item_id', 'data_to_cache'); |
| 100 | |
| 101 | delete() |
| 102 | ======== |
| 103 | |
| 104 | .. php:method:: delete ( $id ) |
| 105 | |
| 106 | This function will delete a specific item from the cache store. If item |
| 107 | deletion fails, the function will return FALSE. |
| 108 | |
| 109 | :param string $id: name of cached item |
| 110 | :returns: TRUE if deleted, FALSE if the deletion fails |
| 111 | :rtype: Boolean |
| 112 | |
| 113 | :: |
| 114 | |
| 115 | $this->cache->delete('cache_item_id'); |
| 116 | |
| 117 | clean() |
| 118 | ======= |
| 119 | |
| 120 | .. php:method:: clean ( ) |
| 121 | |
| 122 | This function will 'clean' the entire cache. If the deletion of the |
| 123 | cache files fails, the function will return FALSE. |
| 124 | |
| 125 | :returns: TRUE if deleted, FALSE if the deletion fails |
| 126 | :rtype: Boolean |
| 127 | |
| 128 | :: |
| 129 | |
| 130 | $this->cache->clean(); |
| 131 | |
| 132 | cache_info() |
| 133 | ============= |
| 134 | |
| 135 | .. php:method:: cache_info ( ) |
| 136 | |
| 137 | This function will return information on the entire cache. |
| 138 | |
| 139 | :returns: information on the entire cache |
| 140 | :rtype: Mixed |
| 141 | |
| 142 | :: |
| 143 | |
| 144 | var_dump($this->cache->cache_info()); |
| 145 | |
| 146 | .. note:: The information returned and the structure of the data is dependent |
| 147 | on which adapter is being used. |
| 148 | |
| 149 | |
| 150 | get_metadata() |
| 151 | =============== |
| 152 | |
| 153 | .. php:method:: get_metadata ( $id ) |
| 154 | |
| 155 | This function will return detailed information on a specific item in the |
| 156 | cache. |
| 157 | |
| 158 | :param string $id: name of cached item |
| 159 | :returns: metadadta for the cached item |
| 160 | :rtype: Mixed |
| 161 | |
| 162 | :: |
| 163 | |
| 164 | var_dump($this->cache->get_metadata('my_cached_item')); |
| 165 | |
| 166 | .. note:: The information returned and the structure of the data is dependent |
| 167 | on which adapter is being used. |
| 168 | |
| 169 | |
| 170 | ******* |
| 171 | Drivers |
| 172 | ******* |
| 173 | |
| 174 | Alternative PHP Cache (APC) Caching |
| 175 | =================================== |
| 176 | |
| 177 | All of the functions listed above can be accessed without passing a |
| 178 | specific adapter to the driver loader as follows:: |
| 179 | |
| 180 | $this->load->driver('cache'); |
| 181 | $this->cache->apc->save('foo', 'bar', 10); |
| 182 | |
| 183 | For more information on APC, please see |
| 184 | `http://php.net/apc <http://php.net/apc>`_ |
| 185 | |
| 186 | File-based Caching |
| 187 | ================== |
| 188 | |
| 189 | Unlike caching from the Output Class, the driver file-based caching |
| 190 | allows for pieces of view files to be cached. Use this with care, and |
| 191 | make sure to benchmark your application, as a point can come where disk |
| 192 | I/O will negate positive gains by caching. |
| 193 | |
| 194 | All of the functions listed above can be accessed without passing a |
| 195 | specific adapter to the driver loader as follows:: |
| 196 | |
| 197 | $this->load->driver('cache'); |
| 198 | $this->cache->file->save('foo', 'bar', 10); |
| 199 | |
| 200 | Memcached Caching |
| 201 | ================= |
| 202 | |
| 203 | Multiple Memcached servers can be specified in the memcached.php |
| 204 | configuration file, located in the application/config/ directory. |
| 205 | |
| 206 | All of the functions listed above can be accessed without passing a |
| 207 | specific adapter to the driver loader as follows:: |
| 208 | |
| 209 | $this->load->driver('cache'); |
| 210 | $this->cache->memcached->save('foo', 'bar', 10); |
| 211 | |
| 212 | For more information on Memcached, please see |
| 213 | `http://php.net/memcached <http://php.net/memcached>`_ |
| 214 | |
| 215 | Dummy Cache |
| 216 | =========== |
| 217 | |
| 218 | This is a caching backend that will always 'miss.' It stores no data, |
| 219 | but lets you keep your caching code in place in environments that don't |
| 220 | support your chosen cache. |