Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^242) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
So now that you’ve been warned, what can caching do for you? In short, everything. The
Zend Framework’s caching solution is great because it allows you to cache not only full pages,
but partial pages and even SQL queries.


Caching Techniques.


The cache component has two key pieces: a front-end and a back-end. The front-end is responsible
for deciding what and how to cache, and the back-end is responsible for storage of the cached
information. Table 16-1 lists the available front-ends, and Table 16-2 lists the back-ends.

Ta b l e 16 - 1. Zend_Cache Front-Ends
Front-End Description
Zend_Cache_Frontend_Core A general-purpose caching front-end with both
load($handle) and save($data) methods. This front-end
defines the base class for all other front-ends, and as such,
it defines a number of useful caching options, including
caching (bool), lifetime (int seconds), logging
(bool log to Zend_Log), write_control, automatic_
serialization, and automatic_cleaning_factor. They are
fairly self-explanatory; see the Zend Framework reference
for more details.
Zend_Cache_Frontend_Output A caching front-end that captures all output between a
$cache->start() and $cache->end() call. It is useful for
caching partial blocks of output, when full-page caching
is not appropriate.
Zend_Cache_Frontend_Function Function caching allows you to save the result of a function as
well as any content it might directly output. It has a single
call(callback $function, array $params) function.
Zend_Cache_Frontend_Class Class caching allows you to cache classes such that state
is remembered between loads. It can be used to serialize/
deserialize objects and prevent you from having to go back
to a database or configuration file to instantiate a class. To
use this front-end, the option 'cachedEntity' must be set
to a name or instance of a class. To call cached methods,
simply call $cache->methodName(...).
Zend_Cache_Frontend_File This is the same as the core front-end but with the exten-
sion that the cache will expire with the modification of a
file. This allows you to cache items such as the parsing of
an XML file indefinitely until the underlying file changes.
To use this front-end, provide an option of
'master_file'=>'/path/to/file'.
Zend_Cache_Frontend_Page This front-end caches an entire page and is quite simple
to use. An option of 'regexps', which is an array of arrays,
declares which pages should be cached. This front-end is
demonstrated in Listing 16-8.

McArthur_819-9C16.fm Page 242 Friday, February 29, 2008 5:07 PM

Free download pdf