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')); |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 23 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | if ( ! $foo = $this->cache->get('foo')) |
| 25 | { |
| 26 | echo 'Saving to the cache!<br />'; |
| 27 | $foo = 'foobarbaz!'; |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 28 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | // Save into the cache for 5 minutes |
| 30 | $this->cache->save('foo', $foo, 300); |
| 31 | } |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 32 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | echo $foo; |
| 34 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 35 | You can also prefix cache item names via the **key_prefix** setting, which is useful |
| 36 | to avoid collisions when you're running multiple applications on the same environment. |
| 37 | |
| 38 | :: |
| 39 | |
| 40 | $this->load->driver('cache', |
| 41 | array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_') |
| 42 | ); |
| 43 | |
| 44 | $this->cache->get('foo'); // Will get the cache entry named 'my_foo' |
| 45 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | ****************** |
| 47 | Function Reference |
| 48 | ****************** |
| 49 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 50 | .. class:: CI_Cache |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
| 52 | is_supported() |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 53 | ============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 55 | .. method:: is_supported ( $driver ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
| 57 | This function is automatically called when accessing drivers via |
| 58 | $this->cache->get(). However, if the individual drivers are used, make |
| 59 | sure to call this function to ensure the driver is supported in the |
| 60 | hosting environment. |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 61 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | :param string $driver: the name of the caching driver |
| 63 | :returns: TRUE if supported, FALSE if not |
| 64 | :rtype: Boolean |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 65 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | :: |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 67 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | if ($this->cache->apc->is_supported() |
| 69 | { |
| 70 | if ($data = $this->cache->apc->get('my_cache')) |
| 71 | { |
| 72 | // do things. |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | get() |
| 78 | ===== |
| 79 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 80 | .. method:: get ( $id ) |
| 81 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 82 | This function will attempt to fetch an item from the cache store. If the |
| 83 | item does not exist, the function will return FALSE. |
| 84 | |
| 85 | :param string $id: name of cached item |
| 86 | :returns: The item if it exists, FALSE if it does not |
| 87 | :rtype: Mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 88 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | :: |
| 90 | |
| 91 | $foo = $this->cache->get('my_cached_item'); |
| 92 | |
| 93 | |
| 94 | save() |
| 95 | ====== |
| 96 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 97 | .. method:: save ( $id , $data [, $ttl]) |
| 98 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | This function will save an item to the cache store. If saving fails, the |
| 100 | function will return FALSE. |
| 101 | |
| 102 | :param string $id: name of the cached item |
| 103 | :param mixed $data: the data to save |
| 104 | :param int $ttl: Time To Live, in seconds (default 60) |
| 105 | :returns: TRUE on success, FALSE on failure |
| 106 | :rtype: Boolean |
| 107 | |
| 108 | :: |
| 109 | |
| 110 | $this->cache->save('cache_item_id', 'data_to_cache'); |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 111 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | delete() |
| 113 | ======== |
| 114 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 115 | .. method:: delete ( $id ) |
| 116 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 117 | This function will delete a specific item from the cache store. If item |
| 118 | deletion fails, the function will return FALSE. |
| 119 | |
| 120 | :param string $id: name of cached item |
| 121 | :returns: TRUE if deleted, FALSE if the deletion fails |
| 122 | :rtype: Boolean |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 123 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | :: |
| 125 | |
| 126 | $this->cache->delete('cache_item_id'); |
| 127 | |
| 128 | clean() |
| 129 | ======= |
| 130 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 131 | .. method:: clean ( ) |
| 132 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | This function will 'clean' the entire cache. If the deletion of the |
| 134 | cache files fails, the function will return FALSE. |
| 135 | |
| 136 | :returns: TRUE if deleted, FALSE if the deletion fails |
| 137 | :rtype: Boolean |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 138 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | :: |
| 140 | |
| 141 | $this->cache->clean(); |
| 142 | |
| 143 | cache_info() |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 144 | ============ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 146 | .. method:: cache_info ( ) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
| 148 | This function will return information on the entire cache. |
| 149 | |
| 150 | :returns: information on the entire cache |
| 151 | :rtype: Mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 152 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | :: |
| 154 | |
| 155 | var_dump($this->cache->cache_info()); |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 156 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | .. note:: The information returned and the structure of the data is dependent |
| 158 | on which adapter is being used. |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 159 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
| 161 | get_metadata() |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 162 | ============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 164 | .. method:: get_metadata ( $id ) |
| 165 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | This function will return detailed information on a specific item in the |
| 167 | cache. |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 168 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 169 | :param string $id: name of cached item |
| 170 | :returns: metadadta for the cached item |
| 171 | :rtype: Mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame^] | 172 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | :: |
| 174 | |
| 175 | var_dump($this->cache->get_metadata('my_cached_item')); |
| 176 | |
| 177 | .. note:: The information returned and the structure of the data is dependent |
| 178 | on which adapter is being used. |
| 179 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 180 | ******* |
| 181 | Drivers |
| 182 | ******* |
| 183 | |
| 184 | Alternative PHP Cache (APC) Caching |
| 185 | =================================== |
| 186 | |
| 187 | All of the functions listed above can be accessed without passing a |
| 188 | specific adapter to the driver loader as follows:: |
| 189 | |
| 190 | $this->load->driver('cache'); |
| 191 | $this->cache->apc->save('foo', 'bar', 10); |
| 192 | |
| 193 | For more information on APC, please see |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 194 | `http://php.net/apc <http://php.net/apc>`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 195 | |
| 196 | File-based Caching |
| 197 | ================== |
| 198 | |
| 199 | Unlike caching from the Output Class, the driver file-based caching |
| 200 | allows for pieces of view files to be cached. Use this with care, and |
| 201 | make sure to benchmark your application, as a point can come where disk |
| 202 | I/O will negate positive gains by caching. |
| 203 | |
| 204 | All of the functions listed above can be accessed without passing a |
| 205 | specific adapter to the driver loader as follows:: |
| 206 | |
| 207 | $this->load->driver('cache'); |
| 208 | $this->cache->file->save('foo', 'bar', 10); |
| 209 | |
| 210 | Memcached Caching |
| 211 | ================= |
| 212 | |
| 213 | Multiple Memcached servers can be specified in the memcached.php |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 214 | configuration file, located in the _application/config/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 215 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 216 | All of the methods listed above can be accessed without passing a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | specific adapter to the driver loader as follows:: |
| 218 | |
| 219 | $this->load->driver('cache'); |
| 220 | $this->cache->memcached->save('foo', 'bar', 10); |
| 221 | |
| 222 | For more information on Memcached, please see |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 223 | `http://php.net/memcached <http://php.net/memcached>`_. |
| 224 | |
| 225 | WinCache Caching |
| 226 | ================ |
| 227 | |
| 228 | Under Windows, you can also utilize the WinCache driver. |
| 229 | |
| 230 | All of the functions listed above can be accessed without passing a |
| 231 | specific adapter to the driver loader as follows:: |
| 232 | |
| 233 | $this->load->driver('cache'); |
| 234 | $this->cache->wincache->save('foo', 'bar', 10); |
| 235 | |
| 236 | For more information on WinCache, please see |
| 237 | `http://php.net/wincache <http://php.net/wincache>`_. |
| 238 | |
| 239 | Redis Caching |
| 240 | ============= |
| 241 | |
| 242 | All of the methods listed above can be accessed without passing a |
| 243 | specific adapter to the driver loader as follows:: |
| 244 | |
| 245 | $this->load->driver('cache'); |
| 246 | $this->cache->redis->save('foo', 'bar', 10); |
| 247 | |
| 248 | .. important:: Redis may require one or more of the following options: |
| 249 | **host**, **post**, **timeout**, **password**. |
| 250 | |
| 251 | The Redis PHP extension repository is located at |
| 252 | `https://github.com/nicolasff/phpredis <https://github.com/nicolasff/phpredis>`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 253 | |
| 254 | Dummy Cache |
| 255 | =========== |
| 256 | |
| 257 | This is a caching backend that will always 'miss.' It stores no data, |
| 258 | but lets you keep your caching code in place in environments that don't |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 259 | support your chosen cache. |