1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.cache.backends">
4 <title>Zend_Cache Backends</title>
7 There are two kinds of backends: standard ones and extended ones. Of course, extended
8 backends offer more features.
11 <sect2 id="zend.cache.backends.file">
12 <title>Zend_Cache_Backend_File</title>
15 This (extended) backends stores cache records into files (in a chosen directory).
19 Available options are :
22 <table id="zend.cache.backends.file.table">
23 <title>File Backend Options</title>
29 <entry>Data Type</entry>
30 <entry>Default Value</entry>
31 <entry>Description</entry>
37 <entry><emphasis>cache_dir</emphasis></entry>
38 <entry><type>String</type></entry>
39 <entry>'<filename>/tmp/</filename>'</entry>
40 <entry>Directory where to store cache files</entry>
44 <entry><emphasis>file_locking</emphasis></entry>
45 <entry><type>Boolean</type></entry>
46 <entry><constant>TRUE</constant></entry>
49 Enable or disable file_locking : Can avoid cache corruption under
50 bad circumstances but it doesn't help on multithread webservers
51 or on <acronym>NFS</acronym> filesystems...
56 <entry><emphasis>read_control</emphasis></entry>
57 <entry><type>Boolean</type></entry>
58 <entry><constant>TRUE</constant></entry>
61 Enable / disable read control : if enabled, a control key is
62 embedded in the cache file and this key is compared with the
63 one calculated after the reading.
68 <entry><emphasis>read_control_type</emphasis></entry>
69 <entry><type>String</type></entry>
70 <entry>'crc32'</entry>
73 Type of read control (only if read control is enabled). Available values
74 are : 'md5' (best but slowest), 'crc32' (lightly less safe but faster,
75 better choice), 'adler32' (new choice, faster than crc32),
76 'strlen' for a length only test (fastest).
81 <entry><emphasis>hashed_directory_level</emphasis></entry>
82 <entry><type>Integer</type></entry>
86 Hashed directory structure level : 0 means "no hashed directory
87 structure", 1 means "one level of directory", 2 means "two levels"...
88 This option can speed up the cache only when you have many thousands of
89 cache files. Only specific benchs can help you to choose the perfect
90 value for you. Maybe, 1 or 2 is a good start.
95 <entry><emphasis>hashed_directory_umask</emphasis></entry>
96 <entry><type>Integer</type></entry>
98 <entry>Umask for the hashed directory structure</entry>
102 <entry><emphasis>file_name_prefix</emphasis></entry>
103 <entry><type>String</type></entry>
104 <entry>'zend_cache'</entry>
107 prefix for cache files ; be really careful with this option because
108 a too generic value in a system cache dir
109 (like <filename>/tmp</filename>) can cause disasters when cleaning
115 <entry><emphasis>cache_file_umask</emphasis></entry>
116 <entry><type>Integer</type></entry>
118 <entry>umask for cache files</entry>
122 <entry><emphasis>metatadatas_array_max_size</emphasis></entry>
123 <entry><type>Integer</type></entry>
127 internal max size for the metadatas array (don't change this value
128 unless you know what you are doing)
136 <sect2 id="zend.cache.backends.sqlite">
137 <title>Zend_Cache_Backend_Sqlite</title>
140 This (extended) backends stores cache records into a SQLite database.
144 Available options are :
147 <table id="zend.cache.backends.sqlite.table">
148 <title>Sqlite Backend Options</title>
153 <entry>Option</entry>
154 <entry>Data Type</entry>
155 <entry>Default Value</entry>
156 <entry>Description</entry>
162 <entry><emphasis>cache_db_complete_path (mandatory)</emphasis></entry>
163 <entry><type>String</type></entry>
164 <entry><constant>NULL</constant></entry>
167 The complete path (filename included) of the SQLite database
172 <entry><emphasis>automatic_vacuum_factor</emphasis></entry>
173 <entry><type>Integer</type></entry>
177 Disable / Tune the automatic vacuum process. The automatic vacuum
178 process defragment the database file (and make it smaller) when a
179 <methodname>clean()</methodname> or <methodname>delete()</methodname>
180 is called: 0 means no automatic vacuum ; 1 means systematic vacuum
181 (when <methodname>delete()</methodname> or
182 <methodname>clean()</methodname> methods are called) ; x (integer) >
183 1 => automatic vacuum randomly 1 times on x
184 <methodname>clean()</methodname> or
185 <methodname>delete()</methodname>.
193 <sect2 id="zend.cache.backends.memcached">
194 <title>Zend_Cache_Backend_Memcached</title>
197 This (extended) backends stores cache records into a memcached server. <ulink
198 url="http://www.danga.com/memcached/">memcached</ulink> is a high-performance,
199 distributed memory object caching system. To use this backend, you need a memcached
200 daemon and <ulink url="http://pecl.php.net/package/memcache">the memcache
201 <acronym>PECL</acronym> extension</ulink>.
205 Be careful : with this backend, "tags" are not supported for the moment as
206 the "doNotTestCacheValidity=true" argument.
210 Available options are :
213 <table id="zend.cache.backends.memcached.table">
214 <title>Memcached Backend Options</title>
219 <entry>Option</entry>
220 <entry>Data Type</entry>
221 <entry>Default Value</entry>
222 <entry>Description</entry>
228 <entry><emphasis>servers</emphasis></entry>
229 <entry><type>Array</type></entry>
232 <command>array(array('host' => 'localhost', 'port' => 11211,
233 'persistent' => true, 'weight' => 1, 'timeout' => 5,
234 'retry_interval' => 15, 'status' => true,
235 'failure_callback' => '' ))</command>
239 An array of memcached servers ; each memcached server is described by
240 an associative array : 'host' => (string) : the name of the memcached
241 server, 'port' => (int) : the port of the memcached server,
242 'persistent' => (bool) : use or not persistent connections to this
243 memcached server 'weight' => (int) :the weight of the memcached
244 server, 'timeout' => (int) :the time out of the memcached server,
245 'retry_interval' => (int) :the retry interval of the memcached server,
246 'status' => (bool) :the status of the memcached server,
247 'failure_callback' => (callback) : the failure_callback of the
253 <entry><emphasis>compression</emphasis></entry>
254 <entry><type>Boolean</type></entry>
255 <entry><constant>FALSE</constant></entry>
258 <constant>TRUE</constant> if you want to use on-the-fly compression
263 <entry><emphasis>compatibility</emphasis></entry>
264 <entry><type>Boolean</type></entry>
265 <entry><constant>FALSE</constant></entry>
268 <constant>TRUE</constant> if you want to use this compatibility mode
269 with old memcache servers or extensions
277 <sect2 id="zend.cache.backends.apc">
278 <title>Zend_Cache_Backend_Apc</title>
281 This (extended) backends stores cache records in shared memory through
282 the <ulink url="http://pecl.php.net/package/APC">APC</ulink> (Alternative
283 <acronym>PHP</acronym> Cache) extension (which is of course need for using this
288 Be careful : with this backend, "tags" are not supported for the moment as
289 the "doNotTestCacheValidity=true" argument.
293 There is no option for this backend.
297 <sect2 id="zend.cache.backends.xcache">
298 <title>Zend_Cache_Backend_Xcache</title>
301 This backends stores cache records in shared memory through
302 the <ulink url="http://xcache.lighttpd.net/">XCache</ulink> extension
303 (which is of course need for using this backend).
307 Be careful : with this backend, "tags" are not supported for the moment as
308 the "doNotTestCacheValidity=true" argument.
312 Available options are :
315 <table id="zend.cache.backends.xcache.table">
316 <title>Xcache Backend Options</title>
321 <entry>Option</entry>
322 <entry>Data Type</entry>
323 <entry>Default Value</entry>
324 <entry>Description</entry>
330 <entry><emphasis>user</emphasis></entry>
331 <entry><type>String</type></entry>
332 <entry><constant>NULL</constant></entry>
335 <filename>xcache.admin.user</filename>, necessary for the
336 <methodname>clean()</methodname> method
341 <entry><emphasis>password</emphasis></entry>
342 <entry><type>String</type></entry>
343 <entry><constant>NULL</constant></entry>
346 <filename>xcache.admin.pass</filename> (in clear form, not
347 <acronym>MD5</acronym>), necessary for the
348 <methodname>clean()</methodname> method
356 <sect2 id="zend.cache.backends.platform">
357 <title>Zend_Cache_Backend_ZendPlatform</title>
360 This backend uses content caching <acronym>API</acronym> of the <ulink
361 url="http://www.zend.com/en/products/platform/">Zend Platform</ulink> product.
362 Naturally, to use this backend you need to have Zend Platform installed.
366 This backend supports tags, but does not support
367 <constant>CLEANING_MODE_NOT_MATCHING_TAG</constant> cleaning mode.
371 Specify this backend using a word separator -- '-', '.', ' ', or '_'
372 -- between the words 'Zend' and 'Platform' when using the
373 <methodname>Zend_Cache::factory()</methodname> method:
376 <programlisting language="php"><![CDATA[
377 $cache = Zend_Cache::factory('Core', 'Zend Platform');
381 There are no options for this backend.
385 <sect2 id="zend.cache.backends.twolevels">
386 <title>Zend_Cache_Backend_TwoLevels</title>
389 This (extend) backend is an hybrid one. It stores cache records in two other backends :
390 a fast one (but limited) like Apc, Memcache... and a "slow" one like File, Sqlite...
394 This backend will use the priority parameter (given at the frontend level when storing a
395 record) and the remaining space in the fast backend to optimize the usage of these two
400 Specify this backend using a word separator -- '-', '.', ' ', or '_'
401 -- between the words 'Two' and 'Levels' when using the
402 <methodname>Zend_Cache::factory()</methodname> method:
405 <programlisting language="php"><![CDATA[
406 $cache = Zend_Cache::factory('Core', 'Two Levels');
410 Available options are :
413 <table id="zend.cache.backends.twolevels.table">
414 <title>TwoLevels Backend Options</title>
419 <entry>Option</entry>
420 <entry>Data Type</entry>
421 <entry>Default Value</entry>
422 <entry>Description</entry>
428 <entry><emphasis>slow_backend</emphasis></entry>
429 <entry><type>String</type></entry>
431 <entry>the "slow" backend name</entry>
435 <entry><emphasis>fast_backend</emphasis></entry>
436 <entry><type>String</type></entry>
438 <entry>the "fast" backend name</entry>
442 <entry><emphasis>slow_backend_options</emphasis></entry>
443 <entry><type>Array</type></entry>
444 <entry><methodname>array()</methodname></entry>
445 <entry>the "slow" backend options</entry>
449 <entry><emphasis>fast_backend_options</emphasis></entry>
450 <entry><type>Array</type></entry>
451 <entry><methodname>array()</methodname></entry>
452 <entry>the "fast" backend options</entry>
456 <entry><emphasis>slow_backend_custom_naming</emphasis></entry>
457 <entry><type>Boolean</type></entry>
458 <entry><constant>FALSE</constant></entry>
461 if <constant>TRUE</constant>, the slow_backend argument is used as a
462 complete class name; if <constant>FALSE</constant>,
463 the frontend argument is used as the end of
464 "<classname>Zend_Cache_Backend_</classname>[...]" class name
469 <entry><emphasis>fast_backend_custom_naming</emphasis></entry>
470 <entry><type>Boolean</type></entry>
471 <entry><constant>FALSE</constant></entry>
474 if <constant>TRUE</constant>, the fast_backend argument is used as a
475 complete class name; if <constant>FALSE</constant>,
476 the frontend argument is used as the end of
477 "<classname>Zend_Cache_Backend_</classname>[...]" class name
482 <entry><emphasis>slow_backend_autoload</emphasis></entry>
483 <entry><type>Boolean</type></entry>
484 <entry><constant>FALSE</constant></entry>
487 if <constant>TRUE</constant>, there will no require_once for the
488 slow backend (useful only for custom backends)
493 <entry><emphasis>fast_backend_autoload</emphasis></entry>
494 <entry><type>Boolean</type></entry>
495 <entry><constant>FALSE</constant></entry>
498 if <constant>TRUE</constant>, there will no require_once for the fast
499 backend (useful only for custom backends)
504 <entry><emphasis>auto_refresh_fast_cache</emphasis></entry>
505 <entry><type>Boolean</type></entry>
506 <entry><constant>TRUE</constant></entry>
509 if <constant>TRUE</constant>, auto refresh the fast cache when a
515 <entry><emphasis>stats_update_factor</emphasis></entry>
516 <entry><type>Integer</type></entry>
520 disable / tune the computation of the fast backend filling percentage
521 (when saving a record into cache, computation of the fast backend
522 filling percentage randomly 1 times on x cache writes)
530 <sect2 id="zend.cache.backends.zendserver">
531 <title>Zend_Cache_Backend_ZendServer_Disk and Zend_Cache_Backend_ZendServer_ShMem</title>
534 These backends store cache records using <ulink
535 url="http://www.zend.com/en/products/server/downloads-all?zfs=zf_download">Zend
536 Server</ulink> caching functionality.
540 Be careful: with these backends, "tags" are not supported for the moment as the
541 "doNotTestCacheValidity=true" argument.
545 These backend work only withing Zend Server environment for pages requested through
546 <acronym>HTTP</acronym> or <acronym>HTTPS</acronym> and don't work for command line
551 Specify this backend using parameter <emphasis>customBackendNaming</emphasis> as
552 <constant>TRUE</constant> when using the <methodname>Zend_Cache::factory()</methodname>
556 <programlisting language="php"><![CDATA[
557 $cache = Zend_Cache::factory('Core', 'Zend_Cache_Backend_ZendServer_Disk',
558 $frontendOptions, $backendOptions, false, true);
562 There is no option for this backend.
566 <sect2 id="zend.cache.backends.static">
567 <title>Zend_Cache_Backend_Static</title>
570 This backend works in concert with <classname>Zend_Cache_Frontend_Capture</classname>
571 (the two must be used together) to save the output from requests as static files. This
572 means the static files are served directly on subsequent requests without any
573 involvement of <acronym>PHP</acronym> or Zend Framework at all.
578 <classname>Zend_Cache_Frontend_Capture</classname> operates
579 by registering a callback function to be called
580 when the output buffering it uses is cleaned. In order for this to operate
581 correctly, it must be the final output buffer in the request. To guarantee
582 this, the output buffering used by the Dispatcher <emphasis>must</emphasis> be
583 disabled by calling <classname>Zend_Controller_Front</classname>'s
584 <methodname>setParam()</methodname> method, for example,
585 <command>$front->setParam('disableOutputBuffering', true);</command> or adding
586 "resources.frontcontroller.params.disableOutputBuffering = true"
587 to your bootstrap configuration file (assumed <acronym>INI</acronym>) if using
588 <classname>Zend_Application</classname>.
593 The benefits of this cache include a large throughput increase since
594 all subsequent requests return the static file and don't need any
595 dynamic processing. Of course this also has some disadvantages. The
596 only way to retry the dynamic request is to purge the cached file
597 from elsewhere in the application (or via a cronjob if timed). It
598 is also restricted to single-server applications where only one
599 filesystem is used. Nevertheless, it can be a powerful means of
600 getting more performance without incurring the cost of a proxy on
605 Before describing its options, you should note this needs some
606 changes to the default <filename>.htaccess</filename> file in order for requests to be
607 directed to the static files if they exist. Here's an example of
608 a simple application caching some content, including two specific
609 feeds which need additional treatment to serve a correct
613 <programlisting language="text"><![CDATA[
614 AddType application/rss+xml .xml
615 AddType application/atom+xml .xml
619 RewriteCond %{REQUEST_URI} feed/rss$
620 RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.xml -f
621 RewriteRule .* cached/%{REQUEST_URI}.xml [L,T=application/rss+xml]
623 RewriteCond %{REQUEST_URI} feed/atom$
624 RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.xml -f
625 RewriteRule .* cached/%{REQUEST_URI}.xml [L,T=application/atom+xml]
627 RewriteCond %{DOCUMENT_ROOT}/cached/index.html -f
628 RewriteRule ^/*$ cached/index.html [L]
629 RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.(html|xml|json|opml|svg) -f
630 RewriteRule .* cached/%{REQUEST_URI}.%1 [L]
632 RewriteCond %{REQUEST_FILENAME} -s [OR]
633 RewriteCond %{REQUEST_FILENAME} -l [OR]
634 RewriteCond %{REQUEST_FILENAME} -d
635 RewriteRule ^.*$ - [NC,L]
637 RewriteRule ^.*$ index.php [NC,L]
641 The above assumes static files are cached to the directory
642 <filename>./public/cached</filename>. We'll cover the option setting this location,
647 Due to the nature of static file caching, the backend class offers two additional
648 methods: <methodname>remove()</methodname> and
649 <methodname>removeRecursively()</methodname>. Both accept a request
650 <acronym>URI</acronym>, which when mapped to the "public_dir" where static files are
651 cached, and has a pre-stored extension appended, provides the name of either a static
652 file to delete, or a directory path to delete recursively. Due to the
653 restraints of <classname>Zend_Cache_Backend_Interface</classname>, all
654 other methods such as <methodname>save()</methodname> accept an ID which
655 is calculated by applying <methodname>bin2hex()</methodname> to a request
656 <acronym>URI</acronym>.
660 Given the level at which static caching operates, static file caching is addressed for
661 simpler use with the <classname>Zend_Controller_Action_Helper_Cache</classname> action
662 helper. This helper assists in setting which actions of a controller to cache, with what
663 tags, and with which extension. It also offers methods for purging the cache by request
664 <acronym>URI</acronym> or tag. Static file caching is also assisted by
665 <classname>Zend_Cache_Manager</classname> which includes pre-configured configuration
666 templates for a static cache (as <constant>Zend_Cache_Manager::PAGECACHE</constant> or
667 "page"). The defaults therein can be configured as needed to set up a "public_dir"
668 location for caching, etc.
673 It should be noted that the static cache actually uses a secondary cache to store
674 tags (obviously we can't store them elsewhere since a static cache does not invoke
675 <acronym>PHP</acronym> if working correctly). This is just a standard Core cache,
676 and should use a persistent backend such as File or TwoLevels (to take advantage of
677 memory storage without sacrificing permanent persistance). The backend includes the
678 option "tag_cache" to set this up (it is obligatory), or the
679 <methodname>setInnerCache()</methodname> method.
683 <table id="zend.cache.backends.static.table">
684 <title>Static Backend Options</title>
689 <entry>Option</entry>
690 <entry>Data Type</entry>
691 <entry>Default Value</entry>
692 <entry>Description</entry>
698 <entry><emphasis>public_dir</emphasis></entry>
699 <entry><type>String</type></entry>
700 <entry><constant>NULL</constant></entry>
702 Directory where to store static files. This must exist
703 in your public directory.
708 <entry><emphasis>file_locking</emphasis></entry>
709 <entry><type>Boolean</type></entry>
710 <entry><constant>TRUE</constant></entry>
713 Enable or disable file_locking : Can avoid cache corruption under
714 bad circumstances but it doesn't help on multithread webservers
715 or on <acronym>NFS</acronym> filesystems...
720 <entry><emphasis>read_control</emphasis></entry>
721 <entry><type>Boolean</type></entry>
722 <entry><constant>TRUE</constant></entry>
725 Enable / disable read control : if enabled, a control key is
726 embedded in the cache file and this key is compared with the
727 one calculated after the reading.
732 <entry><emphasis>read_control_type</emphasis></entry>
733 <entry><type>String</type></entry>
734 <entry>'crc32'</entry>
737 Type of read control (only if read control is enabled). Available values
738 are : 'md5' (best but slowest), 'crc32' (lightly less safe but faster,
739 better choice), 'adler32' (new choice, faster than crc32),
740 'strlen' for a length only test (fastest).
745 <entry><emphasis>cache_file_umask</emphasis></entry>
746 <entry><type>Integer</type></entry>
748 <entry>umask for cached files.</entry>
752 <entry><emphasis>cache_directory_umask</emphasis></entry>
753 <entry><type>Integer</type></entry>
755 <entry>Umask for directories created within public_dir.</entry>
759 <entry><emphasis>file_extension</emphasis></entry>
760 <entry><type>String</type></entry>
761 <entry>'<filename>.html</filename>'</entry>
764 Default file extension for static files created. This can be
765 configured on the fly, see
766 <methodname>Zend_Cache_Backend_Static::save()</methodname> though
767 generally it's recommended to rely on
768 <classname>Zend_Controller_Action_Helper_Cache</classname> when
769 doing so since it's simpler that way than messing with
770 arrays or serialization manually.
775 <entry><emphasis>index_filename</emphasis></entry>
776 <entry><type>String</type></entry>
777 <entry>'index'</entry>
780 If a request <acronym>URI</acronym> does not contain sufficient
781 information to construct a static file (usually this means an index
782 call, e.g. <acronym>URI</acronym> of '/'), the index_filename is used
783 instead. So '' or '/' would map to '<filename>index.html</filename>'
784 (assuming the default file_extension is '<filename>.html</filename>').
789 <entry><emphasis>tag_cache</emphasis></entry>
790 <entry><type>Object</type></entry>
791 <entry><constant>NULL</constant></entry>
794 Used to set an 'inner' cache utilised to store tags
795 and file extensions associated with static files. This
796 <emphasis>must</emphasis> be set or the static cache cannot be tracked
802 <entry><emphasis>disable_caching</emphasis></entry>
803 <entry><type>Boolean</type></entry>
804 <entry><constant>FALSE</constant></entry>
807 If set to <constant>TRUE</constant>, static files will not be cached.
808 This will force all requests to be dynamic even if marked
809 to be cached in Controllers. Useful for debugging.