Zend_Cache review

Zend Framework comes with interesting caching classes. I tested them at this weekend and I´m glad to say that they can speed up your application dramatically. Zend_Cache namespace contains classes that aid in caching page outputs and function results; and, of course, the caching core API allows storing virtually any information you want. What is also brilliant, Zend_Cache allows developers to manage caching records by associating them with tags, which makes possible group operations with the cached items. You can find a few useful examples in the Zend\Cache\EXAMPLES.txt file.

Caching API provides five interfaces for storing data, called "frontends":

  • Core interface supports storing of any PHP structures, such as strings, arrays or objects. This interface is a base for other frontends.
  • Output captures program´s output and saves it as one cache entry.
  • Function interface proxifies calls to functions and caches function results.
  • Class aids in caching method results.
  • Page frontend caches page output using elements of request collections (Cookies, Session, Get, etc.) for building the key. When the other frontends are intended to be used within the controller action, this one can be used before the controller starts.

The cache records storages are called "backends" and the release contains two of them:

  • File stores cache entries as files.
  • Sqlite backend stores cache entries in a SQLite database.
  • Memcached is an upcoming backend, available only in development version now. It is intended to store cache entries in the memory.

Backend implementation looks strange for me, because it does not use the Zend_Db for data access; what should developers do when they want their caching entries to be stored in the MySQL database? I hope it will be changed in the future releases and Zend_Cache developers will provide us with MySQL caching backend.

Here are a few advices concerning using the caching classes that I wrote down during digging:

  1. Use the SQLite backend whenever possible, because File backend creates files using cache entry key as file name. This limits the possible key length.
  2. Create groups of tags using tag prefix ("post_", for example) and tag each cache item with corresponding tags. This will allow you to delete all dependent caching records when the related [database] record is updated or deleted.
  3. Manage the cache: set automaticCleaningFactor option or manually clean up the folder with cached items.
  4. Store frequently accessed resources in the memory, using upcoming Memcached backend.

By caching page output with "Output" cache frontent, I've increased performance of the "index" page rendering in 11 times (getting page from the cache takes 42ms, rendering takes 462ms).