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