While implementing web applications developers often face the necessity of raising efficiency and speed of a resource response. One of the methods for PHP applications speed increase is caching. Presently there are a lot of libraries and modules that implement some scheme of caching. At the same time the situations when one should deny earlier solutions are inevitable. It can happen at any stage of implementation and because of different reasons.
This working with caching is more than timely issue. We will examine using the unified interface for all the caching schemes. It allows to change the caching mechanism without main app modules refactoring as well as to arrange flexible adjustment of caching depending on the current tasks. It is possible to achieve such flexibility while working with cache using Zend_Cache. Zend_Cache is a Zend Framework component and it realizes common interface for caching.
There are three main characteristics in Zend_Cache:
* a unique identifier (a line) that is intended for cache notices identification;
* “lifetime” directive. It defines time when cached data are valid;
* conditional implementation possibility that is used for increasing efficiency by means of ignorance of big code sections.
Cache management in Zend_Cache is realized through the mechanism of front- and backends. Zend_Cache frontends are its interface parts, and they implement the unified cache interface.
There are following frontends in Zend_Cache:
*Zend_Cache_Frontend_Output – it gathers output data. It uses PHP output buffering for saving
everything issued between start() and end() methods;
*Zend_Cache_Frontend_Function – is used for caching the results of functions work. It allows to
define the array of functions for caching;
*Zend_Cache_Frontend_Class – permits to cache objects and static methods results;
*Zend_Cache_Frontend_File – is intended for files caching. It is usually used for configuration files,
patterns caching and so on;
*Zend_Cache_Frontend_Page – used for caching the whole pages. Cache identifier is counted
automatically using global arrays $_SERVER[‘REQUEST_URI’] and $_GET, $_POST, $_SESSION,
$_COOKIE, $_FILES (depending on options). Area of use: static pages and pages that are changed
quite seldom.
Backend mechanism:
*Zend_Cache_Backend_File – keeps cached data in a file;
*Zend_Cache_Backend_Sqlite – is used for keeping cached data in SQLite DB;
*Zend_Cache_Backend_Memcached – stores cache notices using Memcached server;
*Zend_Cache_Backend_Apc – is utilized for working with APC (Alternative PHP Cache);
*Zend_Cache_Backend_Xcache – this backend keeps cache notices in common used storage system
with the help of Xcache extention;
*Zend_Cache_Backend_TwoLevels – it stores cache records in two other backends: a “fast” one (but
limited) like Apc, Memcache and a “slow” one like File, Sqlite
To raise flexibility there is a tag tool realized in Zend_Cache. In other words, when some data are
added to cache there is a possibility to place them as a tag array.
Thus Zend_Cache is a component that provides profound opportunities for unified interface (frontend), and a cache method change is presented by the backend configuration. It serves for creating flexible software and it also allows to organize the adaptable process of its development.