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 | |
Derek Jones | 9fe093e | 2013-07-21 13:47:08 -0700 | [diff] [blame] | 10 | .. contents:: |
| 11 | :local: |
| 12 | |
| 13 | .. raw:: html |
| 14 | |
| 15 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 16 | |
| 17 | ************* |
| 18 | Example Usage |
| 19 | ************* |
| 20 | |
ftwbzhao | f532736 | 2015-05-06 19:34:57 +0800 | [diff] [blame^] | 21 | The following example will load the cache driver, specify `APC <#alternative-php-cache-apc-caching>`_ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | as the driver to use, and fall back to file-based caching if APC is not |
| 23 | available in the hosting environment. |
| 24 | |
| 25 | :: |
| 26 | |
| 27 | $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file')); |
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 | if ( ! $foo = $this->cache->get('foo')) |
| 30 | { |
| 31 | echo 'Saving to the cache!<br />'; |
| 32 | $foo = 'foobarbaz!'; |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 33 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | // Save into the cache for 5 minutes |
| 35 | $this->cache->save('foo', $foo, 300); |
| 36 | } |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 37 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | echo $foo; |
| 39 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 40 | You can also prefix cache item names via the **key_prefix** setting, which is useful |
| 41 | to avoid collisions when you're running multiple applications on the same environment. |
| 42 | |
| 43 | :: |
| 44 | |
| 45 | $this->load->driver('cache', |
| 46 | array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_') |
| 47 | ); |
| 48 | |
| 49 | $this->cache->get('foo'); // Will get the cache entry named 'my_foo' |
| 50 | |
Derek Jones | 9fe093e | 2013-07-21 13:47:08 -0700 | [diff] [blame] | 51 | *************** |
| 52 | Class Reference |
| 53 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 55 | .. php:class:: CI_Cache |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 57 | .. php:method:: is_supported($driver) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 59 | :param string $driver: the name of the caching driver |
| 60 | :returns: TRUE if supported, FALSE if not |
| 61 | :rtype: bool |
Derek Jones | 9fe093e | 2013-07-21 13:47:08 -0700 | [diff] [blame] | 62 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 63 | This method is automatically called when accessing drivers via |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 64 | ``$this->cache->get()``. However, if the individual drivers are used, |
| 65 | make sure to call this method to ensure the driver is supported in the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | hosting environment. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | :: |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 68 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | if ($this->cache->apc->is_supported() |
| 70 | { |
| 71 | if ($data = $this->cache->apc->get('my_cache')) |
| 72 | { |
| 73 | // do things. |
| 74 | } |
| 75 | } |
| 76 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 77 | .. php:method:: get($id) |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 78 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 79 | :param string $id: Cache item name |
| 80 | :returns: Item value or FALSE if not found |
| 81 | :rtype: mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 82 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 83 | This method will attempt to fetch an item from the cache store. If the |
| 84 | item does not exist, the method will return FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | :: |
| 86 | |
| 87 | $foo = $this->cache->get('my_cached_item'); |
| 88 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 89 | .. php:method:: save($id, $data[, $ttl = 60[, $raw = FALSE]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 91 | :param string $id: Cache item name |
| 92 | :param mixed $data: the data to save |
| 93 | :param int $ttl: Time To Live, in seconds (default 60) |
| 94 | :param bool $raw: Whether to store the raw value |
| 95 | :returns: TRUE on success, FALSE on failure |
| 96 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 98 | This method will save an item to the cache store. If saving fails, the |
| 99 | method will return FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | :: |
| 101 | |
| 102 | $this->cache->save('cache_item_id', 'data_to_cache'); |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 103 | |
Andrey Andreev | d0bc7eb | 2014-01-09 17:37:06 +0200 | [diff] [blame] | 104 | .. note:: The ``$raw`` parameter is only utilized by APC and Memcache, |
| 105 | in order to allow usage of ``increment()`` and ``decrement()``. |
| 106 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 107 | .. php:method:: delete($id) |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 108 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 109 | :param string $id: name of cached item |
| 110 | :returns: TRUE on success, FALSE on failure |
| 111 | :rtype: bool |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 112 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 113 | This method will delete a specific item from the cache store. If item |
| 114 | deletion fails, the method will return FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | :: |
| 116 | |
| 117 | $this->cache->delete('cache_item_id'); |
| 118 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 119 | .. php:method:: increment($id[, $offset = 1]) |
Andrey Andreev | d0bc7eb | 2014-01-09 17:37:06 +0200 | [diff] [blame] | 120 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 121 | :param string $id: Cache ID |
| 122 | :param int $offset: Step/value to add |
| 123 | :returns: New value on success, FALSE on failure |
| 124 | :rtype: mixed |
Andrey Andreev | d0bc7eb | 2014-01-09 17:37:06 +0200 | [diff] [blame] | 125 | |
| 126 | Performs atomic incrementation of a raw stored value. |
| 127 | :: |
| 128 | |
| 129 | // 'iterator' has a value of 2 |
| 130 | |
| 131 | $this->cache->increment('iterator'); // 'iterator' is now 3 |
| 132 | |
| 133 | $this->cache->increment('iterator', 3); // 'iterator' is now 6 |
| 134 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 135 | .. php:method:: decrement($id[, $offset = 1]) |
Andrey Andreev | d0bc7eb | 2014-01-09 17:37:06 +0200 | [diff] [blame] | 136 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 137 | :param string $id: Cache ID |
| 138 | :param int $offset: Step/value to reduce by |
| 139 | :returns: New value on success, FALSE on failure |
| 140 | :rtype: mixed |
Andrey Andreev | d0bc7eb | 2014-01-09 17:37:06 +0200 | [diff] [blame] | 141 | |
| 142 | Performs atomic decrementation of a raw stored value. |
| 143 | :: |
| 144 | |
| 145 | // 'iterator' has a value of 6 |
| 146 | |
| 147 | $this->cache->decrement('iterator'); // 'iterator' is now 5 |
| 148 | |
| 149 | $this->cache->decrement('iterator', 2); // 'iterator' is now 3 |
| 150 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 151 | .. php:method:: clean() |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 152 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 153 | :returns: TRUE on success, FALSE on failure |
| 154 | :rtype: bool |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 155 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 156 | This method will 'clean' the entire cache. If the deletion of the |
| 157 | cache files fails, the method will return FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 158 | :: |
| 159 | |
| 160 | $this->cache->clean(); |
| 161 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 162 | .. php:method:: cache_info() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 164 | :returns: Information on the entire cache database |
| 165 | :rtype: mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 166 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 167 | This method will return information on the entire cache. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 168 | :: |
| 169 | |
| 170 | var_dump($this->cache->cache_info()); |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 171 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | .. note:: The information returned and the structure of the data is dependent |
| 173 | on which adapter is being used. |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 174 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 175 | .. php:method:: get_metadata($id) |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 176 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 177 | :param string $id: Cache item name |
| 178 | :returns: Metadata for the cached item |
| 179 | :rtype: mixed |
Derek Jones | ca51e1a | 2013-07-21 13:35:02 -0700 | [diff] [blame] | 180 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 181 | This method will return detailed information on a specific item in the |
Derek Jones | 9fe093e | 2013-07-21 13:47:08 -0700 | [diff] [blame] | 182 | cache. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | :: |
| 184 | |
| 185 | var_dump($this->cache->get_metadata('my_cached_item')); |
| 186 | |
| 187 | .. note:: The information returned and the structure of the data is dependent |
| 188 | on which adapter is being used. |
| 189 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 190 | ******* |
| 191 | Drivers |
| 192 | ******* |
| 193 | |
| 194 | Alternative PHP Cache (APC) Caching |
| 195 | =================================== |
| 196 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 197 | All of the methods listed above can be accessed without passing a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | specific adapter to the driver loader as follows:: |
| 199 | |
| 200 | $this->load->driver('cache'); |
| 201 | $this->cache->apc->save('foo', 'bar', 10); |
| 202 | |
| 203 | For more information on APC, please see |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 204 | `http://php.net/apc <http://php.net/apc>`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 205 | |
| 206 | File-based Caching |
| 207 | ================== |
| 208 | |
| 209 | Unlike caching from the Output Class, the driver file-based caching |
| 210 | allows for pieces of view files to be cached. Use this with care, and |
| 211 | make sure to benchmark your application, as a point can come where disk |
| 212 | I/O will negate positive gains by caching. |
| 213 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 214 | All of the methods listed above can be accessed without passing a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 215 | specific adapter to the driver loader as follows:: |
| 216 | |
| 217 | $this->load->driver('cache'); |
| 218 | $this->cache->file->save('foo', 'bar', 10); |
| 219 | |
| 220 | Memcached Caching |
| 221 | ================= |
| 222 | |
| 223 | Multiple Memcached servers can be specified in the memcached.php |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 224 | configuration file, located in the _application/config/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 225 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 226 | All of the methods listed above can be accessed without passing a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 227 | specific adapter to the driver loader as follows:: |
| 228 | |
| 229 | $this->load->driver('cache'); |
| 230 | $this->cache->memcached->save('foo', 'bar', 10); |
| 231 | |
| 232 | For more information on Memcached, please see |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 233 | `http://php.net/memcached <http://php.net/memcached>`_. |
| 234 | |
| 235 | WinCache Caching |
| 236 | ================ |
| 237 | |
| 238 | Under Windows, you can also utilize the WinCache driver. |
| 239 | |
Derek Jones | 9046f20 | 2013-07-21 13:54:19 -0700 | [diff] [blame] | 240 | All of the methods listed above can be accessed without passing a |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 241 | specific adapter to the driver loader as follows:: |
| 242 | |
| 243 | $this->load->driver('cache'); |
| 244 | $this->cache->wincache->save('foo', 'bar', 10); |
| 245 | |
| 246 | For more information on WinCache, please see |
| 247 | `http://php.net/wincache <http://php.net/wincache>`_. |
| 248 | |
| 249 | Redis Caching |
| 250 | ============= |
| 251 | |
kakysha | c6fc0c8 | 2013-10-28 23:01:08 +0300 | [diff] [blame] | 252 | Redis is an in-memory key-value store which can operate in LRU cache mode. |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 253 | To use it, you need `Redis server and phpredis PHP extension <https://github.com/phpredis/phpredis>`_. |
kakysha | c6fc0c8 | 2013-10-28 23:01:08 +0300 | [diff] [blame] | 254 | |
| 255 | Config options to connect to redis server must be stored in the application/config/redis.php file. |
| 256 | Available options are:: |
| 257 | |
| 258 | $config['socket_type'] = 'tcp'; //`tcp` or `unix` |
| 259 | $config['socket'] = '/var/run/redis.sock'; // in case of `unix` socket type |
| 260 | $config['host'] = '127.0.0.1'; |
| 261 | $config['password'] = NULL; |
| 262 | $config['port'] = 6379; |
| 263 | $config['timeout'] = 0; |
| 264 | |
Andrey Andreev | 58c2b10 | 2012-10-26 14:42:29 +0300 | [diff] [blame] | 265 | All of the methods listed above can be accessed without passing a |
| 266 | specific adapter to the driver loader as follows:: |
| 267 | |
| 268 | $this->load->driver('cache'); |
| 269 | $this->cache->redis->save('foo', 'bar', 10); |
| 270 | |
kakysha | c6fc0c8 | 2013-10-28 23:01:08 +0300 | [diff] [blame] | 271 | For more information on Redis, please see |
| 272 | `http://redis.io <http://redis.io>`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
| 274 | Dummy Cache |
| 275 | =========== |
| 276 | |
| 277 | This is a caching backend that will always 'miss.' It stores no data, |
| 278 | 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] | 279 | support your chosen cache. |