blob: 709171dd331fdd76593a8396d2b1ab67c24b90de [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##############
2Caching Driver
3##############
4
5CodeIgniter features wrappers around some of the most popular forms of
6fast and dynamic caching. All but file-based caching require specific
7server requirements, and a Fatal Exception will be thrown if server
8requirements are not met.
9
Derek Jones9fe093e2013-07-21 13:47:08 -070010.. contents::
11 :local:
12
13.. raw:: html
14
15 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050016
17*************
18Example Usage
19*************
20
21The following example will load the cache driver, specify `APC <#apc>`_
22as the driver to use, and fall back to file-based caching if APC is not
23available in the hosting environment.
24
25::
26
27 $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
Derek Jonesca51e1a2013-07-21 13:35:02 -070028
Derek Jones8ede1a22011-10-05 13:34:52 -050029 if ( ! $foo = $this->cache->get('foo'))
30 {
31 echo 'Saving to the cache!<br />';
32 $foo = 'foobarbaz!';
Derek Jonesca51e1a2013-07-21 13:35:02 -070033
Derek Jones8ede1a22011-10-05 13:34:52 -050034 // Save into the cache for 5 minutes
35 $this->cache->save('foo', $foo, 300);
36 }
Derek Jonesca51e1a2013-07-21 13:35:02 -070037
Derek Jones8ede1a22011-10-05 13:34:52 -050038 echo $foo;
39
Andrey Andreev58c2b102012-10-26 14:42:29 +030040You can also prefix cache item names via the **key_prefix** setting, which is useful
41to 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 Jones9fe093e2013-07-21 13:47:08 -070051***************
52Class Reference
53***************
Derek Jones8ede1a22011-10-05 13:34:52 -050054
Derek Jonesca51e1a2013-07-21 13:35:02 -070055.. class:: CI_Cache
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreevcc042092014-01-03 17:08:27 +020057 .. method:: is_supported($driver)
Derek Jones8ede1a22011-10-05 13:34:52 -050058
Derek Jones9fe093e2013-07-21 13:47:08 -070059 :param string $driver: the name of the caching driver
60 :returns: TRUE if supported, FALSE if not
Derek Jones9fe093e2013-07-21 13:47:08 -070061
Derek Jones9046f202013-07-21 13:54:19 -070062 This method is automatically called when accessing drivers via
Andrey Andreevcc042092014-01-03 17:08:27 +020063 ``$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 Jones8ede1a22011-10-05 13:34:52 -050065 hosting environment.
Derek Jones8ede1a22011-10-05 13:34:52 -050066 ::
Derek Jonesca51e1a2013-07-21 13:35:02 -070067
Derek Jones8ede1a22011-10-05 13:34:52 -050068 if ($this->cache->apc->is_supported()
69 {
70 if ($data = $this->cache->apc->get('my_cache'))
71 {
72 // do things.
73 }
74 }
75
Andrey Andreevcc042092014-01-03 17:08:27 +020076 .. method:: get($id)
Derek Jonesca51e1a2013-07-21 13:35:02 -070077
Derek Jones8ede1a22011-10-05 13:34:52 -050078 :param string $id: name of cached item
79 :returns: The item if it exists, FALSE if it does not
Derek Jonesca51e1a2013-07-21 13:35:02 -070080
Derek Jones9046f202013-07-21 13:54:19 -070081 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 Jones8ede1a22011-10-05 13:34:52 -050083 ::
84
85 $foo = $this->cache->get('my_cached_item');
86
Andrey Andreevcc042092014-01-03 17:08:27 +020087 .. method:: save($id, $data[, $ttl = 60])
Derek Jones8ede1a22011-10-05 13:34:52 -050088
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)
92 :returns: TRUE on success, FALSE on failure
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Derek Jones9046f202013-07-21 13:54:19 -070094 This method will save an item to the cache store. If saving fails, the
95 method will return FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -050096 ::
97
98 $this->cache->save('cache_item_id', 'data_to_cache');
Derek Jonesca51e1a2013-07-21 13:35:02 -070099
Andrey Andreevcc042092014-01-03 17:08:27 +0200100 .. method:: delete($id)
Derek Jonesca51e1a2013-07-21 13:35:02 -0700101
Derek Jones8ede1a22011-10-05 13:34:52 -0500102 :param string $id: name of cached item
103 :returns: TRUE if deleted, FALSE if the deletion fails
Derek Jonesca51e1a2013-07-21 13:35:02 -0700104
Derek Jones9046f202013-07-21 13:54:19 -0700105 This method will delete a specific item from the cache store. If item
106 deletion fails, the method will return FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500107 ::
108
109 $this->cache->delete('cache_item_id');
110
Andrey Andreevcc042092014-01-03 17:08:27 +0200111 .. method:: clean()
Derek Jonesca51e1a2013-07-21 13:35:02 -0700112
Derek Jones8ede1a22011-10-05 13:34:52 -0500113 :returns: TRUE if deleted, FALSE if the deletion fails
Derek Jonesca51e1a2013-07-21 13:35:02 -0700114
Derek Jones9046f202013-07-21 13:54:19 -0700115 This method will 'clean' the entire cache. If the deletion of the
116 cache files fails, the method will return FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500117 ::
118
119 $this->cache->clean();
120
Andrey Andreevcc042092014-01-03 17:08:27 +0200121 .. method:: cache_info()
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Derek Jones8ede1a22011-10-05 13:34:52 -0500123 :returns: information on the entire cache
Derek Jonesca51e1a2013-07-21 13:35:02 -0700124
Derek Jones9046f202013-07-21 13:54:19 -0700125 This method will return information on the entire cache.
Derek Jones8ede1a22011-10-05 13:34:52 -0500126 ::
127
128 var_dump($this->cache->cache_info());
Derek Jonesca51e1a2013-07-21 13:35:02 -0700129
Derek Jones8ede1a22011-10-05 13:34:52 -0500130 .. note:: The information returned and the structure of the data is dependent
131 on which adapter is being used.
Derek Jonesca51e1a2013-07-21 13:35:02 -0700132
Andrey Andreevcc042092014-01-03 17:08:27 +0200133 .. method:: get_metadata($id)
Derek Jonesca51e1a2013-07-21 13:35:02 -0700134
Derek Jones8ede1a22011-10-05 13:34:52 -0500135 :param string $id: name of cached item
136 :returns: metadadta for the cached item
Derek Jonesca51e1a2013-07-21 13:35:02 -0700137
Derek Jones9046f202013-07-21 13:54:19 -0700138 This method will return detailed information on a specific item in the
Derek Jones9fe093e2013-07-21 13:47:08 -0700139 cache.
Derek Jones8ede1a22011-10-05 13:34:52 -0500140 ::
141
142 var_dump($this->cache->get_metadata('my_cached_item'));
143
144 .. note:: The information returned and the structure of the data is dependent
145 on which adapter is being used.
146
Derek Jones8ede1a22011-10-05 13:34:52 -0500147*******
148Drivers
149*******
150
151Alternative PHP Cache (APC) Caching
152===================================
153
Derek Jones9046f202013-07-21 13:54:19 -0700154All of the methods listed above can be accessed without passing a
Derek Jones8ede1a22011-10-05 13:34:52 -0500155specific adapter to the driver loader as follows::
156
157 $this->load->driver('cache');
158 $this->cache->apc->save('foo', 'bar', 10);
159
160For more information on APC, please see
Andrey Andreev58c2b102012-10-26 14:42:29 +0300161`http://php.net/apc <http://php.net/apc>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
163File-based Caching
164==================
165
166Unlike caching from the Output Class, the driver file-based caching
167allows for pieces of view files to be cached. Use this with care, and
168make sure to benchmark your application, as a point can come where disk
169I/O will negate positive gains by caching.
170
Derek Jones9046f202013-07-21 13:54:19 -0700171All of the methods listed above can be accessed without passing a
Derek Jones8ede1a22011-10-05 13:34:52 -0500172specific adapter to the driver loader as follows::
173
174 $this->load->driver('cache');
175 $this->cache->file->save('foo', 'bar', 10);
176
177Memcached Caching
178=================
179
180Multiple Memcached servers can be specified in the memcached.php
Andrey Andreev58c2b102012-10-26 14:42:29 +0300181configuration file, located in the _application/config/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
Andrey Andreev58c2b102012-10-26 14:42:29 +0300183All of the methods listed above can be accessed without passing a
Derek Jones8ede1a22011-10-05 13:34:52 -0500184specific adapter to the driver loader as follows::
185
186 $this->load->driver('cache');
187 $this->cache->memcached->save('foo', 'bar', 10);
188
189For more information on Memcached, please see
Andrey Andreev58c2b102012-10-26 14:42:29 +0300190`http://php.net/memcached <http://php.net/memcached>`_.
191
192WinCache Caching
193================
194
195Under Windows, you can also utilize the WinCache driver.
196
Derek Jones9046f202013-07-21 13:54:19 -0700197All of the methods listed above can be accessed without passing a
Andrey Andreev58c2b102012-10-26 14:42:29 +0300198specific adapter to the driver loader as follows::
199
200 $this->load->driver('cache');
201 $this->cache->wincache->save('foo', 'bar', 10);
202
203For more information on WinCache, please see
204`http://php.net/wincache <http://php.net/wincache>`_.
205
206Redis Caching
207=============
208
209All of the methods listed above can be accessed without passing a
210specific adapter to the driver loader as follows::
211
212 $this->load->driver('cache');
213 $this->cache->redis->save('foo', 'bar', 10);
214
215.. important:: Redis may require one or more of the following options:
216 **host**, **post**, **timeout**, **password**.
217
218The Redis PHP extension repository is located at
219`https://github.com/nicolasff/phpredis <https://github.com/nicolasff/phpredis>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
221Dummy Cache
222===========
223
224This is a caching backend that will always 'miss.' It stores no data,
225but lets you keep your caching code in place in environments that don't
Andrey Andreev58c2b102012-10-26 14:42:29 +0300226support your chosen cache.