Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


This backend works in concert with Zend_Cache_Frontend_Capture (the two must be used together) to save the output from requests as static files. This means the static files are served directly on subsequent requests without any involvement of PHP or Zend Framework at all.


Zend_Cache_Frontend_Capture operates by registering a callback function to be called when the output buffering it uses is cleaned. In order for this to operate correctly, it must be the final output buffer in the request. To guarantee this, the output buffering used by the Dispatcher must be disabled by calling Zend_Controller_Front's setParam() method, for example, $front->setParam('disableOutputBuffering', true); or adding "resources.frontcontroller.params.disableOutputBuffering = true" to your bootstrap configuration file (assumed INI) if using Zend_Application.

The benefits of this cache include a large throughput increase since all subsequent requests return the static file and don't need any dynamic processing. Of course this also has some disadvantages. The only way to retry the dynamic request is to purge the cached file from elsewhere in the application (or via a cronjob if timed). It is also restricted to single-server applications where only one filesystem is used. Nevertheless, it can be a powerful means of getting more performance without incurring the cost of a proxy on single machines.

Before describing its options, you should note this needs some changes to the default .htaccess file in order for requests to be directed to the static files if they exist. Here's an example of a simple application caching some content, including two specific feeds which need additional treatment to serve a correct Content-Type header:

AddType application/rss+xml .xml
AddType application/atom+xml .xml

RewriteEngine On

RewriteCond %{REQUEST_URI} feed/rss$
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.xml -f
RewriteRule .* cached/%{REQUEST_URI}.xml [L,T=application/rss+xml]

RewriteCond %{REQUEST_URI} feed/atom$
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.xml -f
RewriteRule .* cached/%{REQUEST_URI}.xml [L,T=application/atom+xml]

RewriteCond %{DOCUMENT_ROOT}/cached/index.html -f
RewriteRule ^/*$ cached/index.html [L]
RewriteCond %{DOCUMENT_ROOT}/cached/%{REQUEST_URI}.(html|xml|json|opml|svg) -f
RewriteRule .* cached/%{REQUEST_URI}.%1 [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]

The above assumes static files are cached to the directory ./public/cached. We'll cover the option setting this location, "public_dir", below.

Due to the nature of static file caching, the backend class offers two additional methods: remove() and removeRecursively(). Both accept a request URI, which when mapped to the "public_dir" where static files are cached, and has a pre-stored extension appended, provides the name of either a static file to delete, or a directory path to delete recursively. Due to the restraints of Zend_Cache_Backend_Interface, all other methods such as save() accept an ID which is calculated by applying bin2hex() to a request URI.

Given the level at which static caching operates, static file caching is addressed for simpler use with the Zend_Controller_Action_Helper_Cache action helper. This helper assists in setting which actions of a controller to cache, with what tags, and with which extension. It also offers methods for purging the cache by request URI or tag. Static file caching is also assisted by Zend_Cache_Manager which includes pre-configured configuration templates for a static cache (as Zend_Cache_Manager::PAGECACHE or "page"). The defaults therein can be configured as needed to set up a "public_dir" location for caching, etc.


It should be noted that the static cache actually uses a secondary cache to store tags (obviously we can't store them elsewhere since a static cache does not invoke PHP if working correctly). This is just a standard Core cache, and should use a persistent backend such as File or TwoLevels (to take advantage of memory storage without sacrificing permanent persistance). The backend includes the option "tag_cache" to set this up (it is obligatory), or the setInnerCache() method.

Table 31. Static Backend Options

Option Data Type Default Value Description
public_dir String NULL Directory where to store static files. This must exist in your public directory.
file_locking Boolean TRUE Enable or disable file_locking : Can avoid cache corruption under bad circumstances but it doesn't help on multithread webservers or on NFS filesystems...
read_control Boolean TRUE Enable / disable read control : if enabled, a control key is embedded in the cache file and this key is compared with the one calculated after the reading.
read_control_type String 'crc32' Type of read control (only if read control is enabled). Available values are : 'md5' (best but slowest), 'crc32' (lightly less safe but faster, better choice), 'adler32' (new choice, faster than crc32), 'strlen' for a length only test (fastest).
cache_file_umask Integer 0600 umask for cached files.
cache_directory_umask Integer 0700 Umask for directories created within public_dir.
file_extension String '.html' Default file extension for static files created. This can be configured on the fly, see Zend_Cache_Backend_Static::save() though generally it's recommended to rely on Zend_Controller_Action_Helper_Cache when doing so since it's simpler that way than messing with arrays or serialization manually.
index_filename String 'index' If a request URI does not contain sufficient information to construct a static file (usually this means an index call, e.g. URI of '/'), the index_filename is used instead. So '' or '/' would map to 'index.html' (assuming the default file_extension is '.html').
tag_cache Object NULL Used to set an 'inner' cache utilised to store tags and file extensions associated with static files. This must be set or the static cache cannot be tracked and managed.
disable_caching Boolean FALSE If set to TRUE, static files will not be cached. This will force all requests to be dynamic even if marked to be cached in Controllers. Useful for debugging.

Zend Framework