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