1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
6 <title>Caching Driver : CodeIgniter User Guide
</title>
8 <style type='text/css' media='all'
>@import url('../userguide.css');
</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css'
/>
11 <script type=
"text/javascript" src=
"../nav/nav.js"></script>
12 <script type=
"text/javascript" src=
"../nav/prototype.lite.js"></script>
13 <script type=
"text/javascript" src=
"../nav/moo.fx.js"></script>
14 <script type=
"text/javascript" src=
"../nav/user_guide_menu.js"></script>
16 <meta http-equiv='expires' content='-
1'
/>
17 <meta http-equiv= 'pragma' content='no-cache'
/>
18 <meta name='robots' content='all'
/>
19 <meta name='author' content='ExpressionEngine Dev Team'
/>
20 <meta name='description' content='CodeIgniter User Guide'
/>
25 <!-- START NAVIGATION -->
26 <div id=
"nav"><div id=
"nav_inner"><script type=
"text/javascript">create_menu('../');
</script></div></div>
27 <div id=
"nav2"><a name=
"top"></a><a href=
"javascript:void(0);" onclick=
"myHeight.toggle();"><img src=
"../images/nav_toggle_darker.jpg" width=
"154" height=
"43" border=
"0" title=
"Toggle Table of Contents" alt=
"Toggle Table of Contents" /></a></div>
29 <table cellpadding=
"0" cellspacing=
"0" border=
"0" style=
"width:100%">
31 <td><h1>CodeIgniter User Guide Version
2.1.3</h1></td>
32 <td id=
"breadcrumb_right"><a href=
"../toc.html">Table of Contents Page
</a></td>
36 <!-- END NAVIGATION -->
39 <!-- START BREADCRUMB -->
40 <table cellpadding=
"0" cellspacing=
"0" border=
"0" style=
"width:100%">
43 <a href=
"http://codeigniter.com/">CodeIgniter Home
</a> ›
44 <a href=
"../index.html">User Guide Home
</a> ›
45 <a href=
"../general/drivers.html">Drivers
</a> ›
48 <td id=
"searchbox"><form method=
"get" action=
"http://www.google.com/search"><input type=
"hidden" name=
"as_sitesearch" id=
"as_sitesearch" value=
"codeigniter.com/user_guide/" />Search User Guide
<input type=
"text" class=
"input" style=
"width:200px;" name=
"q" id=
"q" size=
"31" maxlength=
"255" value=
"" /> <input type=
"submit" class=
"submit" name=
"sa" value=
"Go" /></form></td>
51 <!-- END BREADCRUMB -->
56 <!-- START CONTENT -->
59 <h1>Caching Driver
</h1>
61 <p>CodeIgniter features wrappers around some of the most popular forms of fast and dynamic caching. All but file-based caching require specific server requirements, and a Fatal Exception will be thrown if server requirements are not met.
</p>
63 <h2>Table of Contents
</h2>
65 <li><a href=
"#example_usage" title=
"Example Usage">Example Usage
</a></li>
66 <li><a href=
"#function_reference" title=
"Function Reference">Function Reference
</a></li>
69 <h3>Available Drivers
</h3>
71 <li><a href=
"#apc" title=
"APC Cache">Alternative PHP Cache (APC) Caching
</a></li>
72 <li><a href=
"#file" title=
"File Caching">File-based Caching
</a></li>
73 <li><a href=
"#memcached" title=
"Memcached">Memcached Caching
</a></li>
74 <li><a href=
"#dummy" title=
"Dummy Caching">Dummy Cache
</a></li>
77 <h2 id=
"example_usage">Example Usage
</h2>
79 <p>The following example will load the cache driver, specify
<a href=
"#apc" title=
"APC">APC
</a> as the driver to use, and fall back to file-based caching if APC is not available in the hosting environment.
</p>
82 $this-
>load-
>driver('cache', array('adapter' =
> 'apc', 'backup' =
> 'file'));
<br />
84 if ( ! $foo = $this-
>cache-
>get('foo'))
<br />
86 echo 'Saving to the cache!
<br
/>';
<br />
87 $foo = 'foobarbaz!';
<br />
89 // Save into the cache for
5 minutes
<br />
90 $this-
>cache-
>save('foo', $foo,
300);
<br />
96 <h1 id=
"function_reference">Function Reference
</h1>
98 <h2>is_supported(
<var>driver
</var>['string'])
</h2>
100 <p>This function is automatically called when accessing drivers via
<samp>$this-
>cache-
>get()
</samp>. However, if the individual drivers are used, make sure to call this function to ensure the driver is supported in the hosting environment.
</p>
103 if ($this-
>cache-
>apc-
>is_supported())
<br />
105 if ($data = $this-
>cache-
>apc-
>get('my_cache'))
<br />
106 {
<br />
107 // do things.
<br />
108 }
<br />
112 <h2>get(
<var>id
</var>['string'])
</h2>
114 <p>This function will attempt to fetch an item from the cache store. If the item does not exist, the function will return
<samp>FALSE
</samp>.
</p>
115 <code>$foo = $this-
>cache-
>get('my_cached_item');
</code>
117 <h2>save(
<var>id
</var>['string'],
<var>data
</var>['mixed'],
<var>ttl
</var>['int'])
</h2>
119 <p>This function will save an item to the cache store. If saving fails, the function will return
<samp>FALSE
</samp>.
</p>
120 <p>The optional third parameter (Time To Live) defaults to
60 seconds.
</p>
121 <code>$this-
>cache-
>save('cache_item_id', 'data_to_cache');
</code>
123 <h2>delete(
<var>id
</var>['string'])
</h2>
125 <p>This function will delete a specific item from the cache store. If item deletion fails, the function will return
<samp>FALSE
</samp>.
</p>
126 <code>$this-
>cache-
>delete('cache_item_id');
</code>
130 <p>This function will 'clean' the entire cache. If the deletion of the cache files fails, the function will return
<samp>FALSE
</samp>.
</p>
132 <code>$this-
>cache-
>clean();
</code>
134 <h2>cache_info()
</h2>
136 <p>This function will return information on the entire cache.
</p>
138 <code>var_dump($this-
>cache-
>cache_info());
</code>
140 <h2>get_metadata(
<var>id
</var>['string'])
</h2>
142 <p>This function will return detailed information on a specific item in the cache.
</p>
144 <code>var_dump($this-
>cache-
>get_metadata('my_cached_item'));
</code>
148 <h2 id=
"apc">Alternative PHP Cache (APC) Caching
</h2>
150 <p>All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
</p>
151 <code>$this-
>load-
>driver('cache');
<br />
152 $this-
>cache-
>apc-
>save('foo', 'bar',
10);
</code>
153 <p>For more information on APC, please see
<a href=
"http://php.net/apc">http://php.net/apc
</a></p>
155 <h2 id=
"file">File-based Caching
</h2>
157 <p>Unlike caching from the Output Class, the driver file-based caching allows for pieces of view files to be cached. Use this with care, and make sure to benchmark your application, as a point can come where disk I/O will negate positive gains by caching.
</p>
159 <p>All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
</p>
160 <code>$this-
>load-
>driver('cache');
<br />
161 $this-
>cache-
>file-
>save('foo', 'bar',
10);
</code>
163 <h2 id=
"memcached">Memcached Caching
</h2>
165 <p>Multiple Memcached servers can be specified in the memcached.php configuration file, located in the
<samp>application/config/
</samp> directory.
167 <p>All of the functions listed above can be accessed without passing a specific adapter to the driver loader as follows:
</p>
168 <code>$this-
>load-
>driver('cache');
<br />
169 $this-
>cache-
>memcached-
>save('foo', 'bar',
10);
</code>
171 <p>For more information on Memcached, please see
<a href=
"http://php.net/memcached">http://php.net/memcached
</a></p>
173 <h2 id=
"dummy">Dummy Cache
</h2>
175 <p>This is a caching backend that will always 'miss.' It stores no data, but lets you keep your caching code in place in environments that don't support your chosen cache.
</p>
183 Previous Topic:
<a href=
"zip.html">Zip Encoding Class
</a>
184 ·
185 <a href=
"#top">Top of Page
</a> ·
186 <a href=
"../index.html">User Guide Home
</a> ·
187 Next Topic:
<a href=
"../database/index.html">Database Class
</a>
189 <p><a href=
"http://codeigniter.com">CodeIgniter
</a> · Copyright
© 2006 -
2012 · <a href=
"http://ellislab.com/">EllisLab, Inc.
</a></p>