Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^243

The easiest type of caching to do is full-page caching. It’s really simple, because you have
no inputs and typically have really static content. To enable this type of caching, create a writable
directory called cache in your application path and follow the example in Listing 16-8.

Listing 16-8. Caching Your Front Page (in index.php)

$cacheBackendOptions = array(
'cache_dir' => APP_PATH. '/cache'
);

$cacheFrontendOptions = array(
'regexps' => array(
'^/$' => array() //use default caching options on front page
)
);

$cache = Zend_Cache::factory(
'Page', //Use the page frontend
'File', //Use the file backend
$cacheFrontendOptions,
$cacheBackendOptions
);
$cache->start();

Table 16-2. Zend_Cache Back-Ends
Back-End Description
Zend_Cache_Backend_File The basic back-end, which saves cache objects to a file. You
can set the cache file creation directory with the back-end
option 'cache_dir'. The cache directory must be writable
by the web server user.
Zend_Cache_Backend_Sqlite A back-end that stores the cache items to a SQLite
database file. Use the back-end option
'cache_db_complete_path'=>'/path/to/sqlitefile'
to specify your database.
Zend_Cache_Backend_Memcached A back-end that stores the cache into a memcached daemon.
To enable it, you must provide the servers back-end option,
which is an array of arrays with keys of host, port, and
persistent. You can also enable on-the-fly compression
with the compression back-end option set to Boolean true.
Zend_Cache_Backend_Apc The APC cache back-end lets you use the APC extension to
store cache data. You will need the APC extension, which is
usually not standard on PHP installations. This back-end
requires no extra options.
Zend_Cache_Backend_ZendPlatform You can also use the Zend Platform for caching. This back-
end requires no extra options.

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

Free download pdf