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

Zend Framework 101: Zend_Cache

Beginning With Zend_Cache

Setting up your web application to use Zend_Cache involves the configuration of a front-end and a back-end driver.

The front-end is used to determine exactly which data is to be cached, while the back-end deals with the actual storage of the cached data. You can develop your own front-ends and back-ends, but typically you will not need to.

If your cache is to be used in various locations within your web application, a good strategy will be to create the cache when bootstrapping your application.


The following front-ends are available:

  • Zend_Cache_Core - This is the "main" front-end from which all other front-ends extend from. This is what you typically use for storing arbitrary data or variables.
  • Zend_Cache_Frontend_Page - This front-end captures all output from your PHP script. Next time somebody accesses the page it will returned the save data rather the processing the script.
  • Zend_Cache_Frontend_Output - This is similar to the page front-end, except it will save arbitrary output in your PHP script (as opposed to all of the output).
  • Zend_Cache_Frontend_File - This front-end uses a "master file" as its indicator when the cache needs to be refreshed.
  • Zend_Cache_Frontend_Function - This front-end can be used as a proxy to most PHP functions. It will call the function you specify and save its response for future use.
  • Zend_Cache_Frontend_Class - This front-end is similar to the function front-end, but allows caching of object and static method calls.

In my opinion, the function and class caches should not typically be used - if you develop a function or class that is expensive to run, then that code should be sufficiently optimized to leverage caching itself internally.

Tip: Many of the Zend Framework components (such as Zend_Locale) allow you to specify a cache that they can use internally. You can follow this practice yourself with your own expensive classes.


There are a number of different back-ends available. We're going to use the Zend_Cache_Backend_File back-end in this article, which is used for writing cache data to the filesystem.

Some of the other available back-ends include:

  • Zend_Cache_Backend_Sqlite - Stores cache records in a sqlite database.
  • Zend_Cache_Backend_Memcached - Store cache records in memory.
  • Zend_Cache_Backend_Apc - Lets the APC extension for PHP deal with storing data.

For a comprehensive list of the available back-ends, refer to

Creating a Cache

In order to create a cache, you call the Zend_Cache::factory() method. When calling this method, you must specify which front-end and back-end drivers to use, as well as options for both of them.

As mentioned previously, we will use the Zend_Cache_Backend_File back-end. You can specify this by using File as the name of the driver when calling Zend_Cache::factory(). The only parameter this driver needs is the filesystem path where cache records will be saved. This path must be writable by the web server.

To get started we will use the Zend_Cache_Core front-end driver. This is specified using Core when calling Zend_Cache::factory().

There are many parameters you can specify for the front-end (see for a list), such as the caching lifetime (the default is one hour). For now we'll just enable the automatic_serialization option. Doing so allows us to cache a PHP variable (such as an array) and Zend_Cache will make this data storable automatically.

The following listing shows how to create a cache. The returned object is an instance of Zend_Cache_Core which you can then use throughout your web application.

Listing 3 Creating a cache (listing-3.php)
    $frontendOptions = array(
        'automatic_serialization' => true
    $backendOptions = array(
        'cache_dir' => './cache'
    $cache = Zend_Cache::factory(

Even though this script doesn't yet do anything useful, you will be able to see if your cache is working when you run the script. Specifically, if the filesystem path is not writable, a PHP warning will appear.

In This Article