blob: 8d7b4c440771a937d766240182e517d8852a76d9 [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
10.. contents:: Table of Contents
11
12*************
13Example Usage
14*************
15
16The following example will load the cache driver, specify `APC <#apc>`_
17as the driver to use, and fall back to file-based caching if APC is not
18available in the hosting environment.
19
20::
21
22 $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
23
24 if ( ! $foo = $this->cache->get('foo'))
25 {
26 echo 'Saving to the cache!<br />';
27 $foo = 'foobarbaz!';
28
29 // Save into the cache for 5 minutes
30 $this->cache->save('foo', $foo, 300);
31 }
32
33 echo $foo;
34
Andrey Andreev58c2b102012-10-26 14:42:29 +030035You can also prefix cache item names via the **key_prefix** setting, which is useful
36to 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 Jones8ede1a22011-10-05 13:34:52 -050046******************
47Function Reference
48******************
49
50.. php:class:: CI_Cache
51
52is_supported()
Andrey Andreev58c2b102012-10-26 14:42:29 +030053==============
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55 .. php:method:: is_supported ( $driver )
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.
61
62 :param string $driver: the name of the caching driver
63 :returns: TRUE if supported, FALSE if not
64 :rtype: Boolean
65
66 ::
67
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
77get()
78=====
79
80 .. php:method:: get ( $id )
81
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
88
89 ::
90
91 $foo = $this->cache->get('my_cached_item');
92
93
94save()
95======
96
97 .. php:method:: save ( $id , $data [, $ttl])
98
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');
111
112delete()
113========
114
115 .. php:method:: delete ( $id )
116
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
123
124 ::
125
126 $this->cache->delete('cache_item_id');
127
128clean()
129=======
130
131 .. php:method:: clean ( )
132
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
138
139 ::
140
141 $this->cache->clean();
142
143cache_info()
Andrey Andreev58c2b102012-10-26 14:42:29 +0300144============
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
146 .. php:method:: cache_info ( )
147
148 This function will return information on the entire cache.
149
150 :returns: information on the entire cache
151 :rtype: Mixed
152
153 ::
154
155 var_dump($this->cache->cache_info());
156
157 .. note:: The information returned and the structure of the data is dependent
158 on which adapter is being used.
159
160
161get_metadata()
Andrey Andreev58c2b102012-10-26 14:42:29 +0300162==============
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
164 .. php:method:: get_metadata ( $id )
165
166 This function will return detailed information on a specific item in the
167 cache.
168
169 :param string $id: name of cached item
170 :returns: metadadta for the cached item
171 :rtype: Mixed
172
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 Jones8ede1a22011-10-05 13:34:52 -0500180*******
181Drivers
182*******
183
184Alternative PHP Cache (APC) Caching
185===================================
186
187All of the functions listed above can be accessed without passing a
188specific adapter to the driver loader as follows::
189
190 $this->load->driver('cache');
191 $this->cache->apc->save('foo', 'bar', 10);
192
193For more information on APC, please see
Andrey Andreev58c2b102012-10-26 14:42:29 +0300194`http://php.net/apc <http://php.net/apc>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
196File-based Caching
197==================
198
199Unlike caching from the Output Class, the driver file-based caching
200allows for pieces of view files to be cached. Use this with care, and
201make sure to benchmark your application, as a point can come where disk
202I/O will negate positive gains by caching.
203
204All of the functions listed above can be accessed without passing a
205specific adapter to the driver loader as follows::
206
207 $this->load->driver('cache');
208 $this->cache->file->save('foo', 'bar', 10);
209
210Memcached Caching
211=================
212
213Multiple Memcached servers can be specified in the memcached.php
Andrey Andreev58c2b102012-10-26 14:42:29 +0300214configuration file, located in the _application/config/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Andrey Andreev58c2b102012-10-26 14:42:29 +0300216All of the methods listed above can be accessed without passing a
Derek Jones8ede1a22011-10-05 13:34:52 -0500217specific adapter to the driver loader as follows::
218
219 $this->load->driver('cache');
220 $this->cache->memcached->save('foo', 'bar', 10);
221
222For more information on Memcached, please see
Andrey Andreev58c2b102012-10-26 14:42:29 +0300223`http://php.net/memcached <http://php.net/memcached>`_.
224
225WinCache Caching
226================
227
228Under Windows, you can also utilize the WinCache driver.
229
230All of the functions listed above can be accessed without passing a
231specific adapter to the driver loader as follows::
232
233 $this->load->driver('cache');
234 $this->cache->wincache->save('foo', 'bar', 10);
235
236For more information on WinCache, please see
237`http://php.net/wincache <http://php.net/wincache>`_.
238
239Redis Caching
240=============
241
242All of the methods listed above can be accessed without passing a
243specific 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
251The Redis PHP extension repository is located at
252`https://github.com/nicolasff/phpredis <https://github.com/nicolasff/phpredis>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500253
254Dummy Cache
255===========
256
257This is a caching backend that will always 'miss.' It stores no data,
258but lets you keep your caching code in place in environments that don't
Andrey Andreev58c2b102012-10-26 14:42:29 +0300259support your chosen cache.