PhpRiot
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_Cache_Frontend_Page

Introduction

Zend_Cache_Frontend_Page is like Zend_Cache_Frontend_Output but designed for a complete page. It's impossible to use Zend_Cache_Frontend_Page for caching only a single block.

On the other hand, the "cache id" is calculated automatically with $_SERVER['REQUEST_URI'] and (depending on options) $_GET, $_POST, $_SESSION, $_COOKIE, $_FILES. More over, you have only one method to call (start()) because the end() call is fully automatic when the page is ended.

For the moment, it's not implemented but we plan to add a HTTP conditional system to save bandwidth (the system will send a HTTP 304 Not Modified if the cache is hit and if the browser has already the good version).

Note

This frontend operates by registering a callback function to be called when the output buffering it uses is cleaned. In order for this to operate correctly, it must be the final output buffer in the request. To guarantee this, the output buffering used by the Dispatcher must be disabled by calling Zend_Controller_Front's setParam() method, for example, $front->setParam('disableOutputBuffering', true); or adding "resources.frontcontroller.params.disableOutputBuffering = true" to your bootstrap configuration file (assumed INI) if using Zend_Application.

Available Options

Table 24. Page Frontend Options

Option Data Type Default Value Description
http_conditional Boolean FALSE use the http_conditional system (not implemented for the moment)
debug_header Boolean FALSE if TRUE, a debug text is added before each cached pages
default_options Array array(...see below...) an associative array of default options:
  • (boolean, TRUE by default) cache: cache is on if TRUE

  • (boolean, FALSE by default) cache_with_get_variables: if TRUE, cache is still on even if there are some variables in $_GET array

  • (boolean, FALSE by default) cache_with_post_variables: if TRUE, cache is still on even if there are some variables in $_POST array

  • (boolean, FALSE by default) cache_with_session_variables: if TRUE, cache is still on even if there are some variables in $_SESSION array

  • (boolean, FALSE by default) cache_with_files_variables: if TRUE, cache is still on even if there are some variables in $_FILES array

  • (boolean, FALSE by default) cache_with_cookie_variables: if TRUE, cache is still on even if there are some variables in $_COOKIE array

  • (boolean, TRUE by default) make_id_with_get_variables: if TRUE, the cache id will be dependent of the content of the $_GET array

  • (boolean, TRUE by default) make_id_with_post_variables: if TRUE, the cache id will be dependent of the content of the $_POST array

  • (boolean, TRUE by default) make_id_with_session_variables: if TRUE, the cache id will be dependent of the content of the $_SESSION array

  • (boolean, TRUE by default) make_id_with_files_variables: if TRUE, the cache id will be dependent of the content of the $_FILES array

  • (boolean, TRUE by default) make_id_with_cookie_variables: if TRUE, the cache id will be dependent of the content of the $_COOKIE array

  • (int, FALSE by default) specific_lifetime: if not FALSE, the given lifetime will be used for the chosen regexp

  • (array, array() by default) tags: tags for the cache record

  • (int, NULL by default) priority: priority (if the backend supports it)

regexps Array array() an associative array to set options only for some REQUEST_URI, keys are (PCRE) regexps, values are associative arrays with specific options to set if the regexp matchs on $_SERVER['REQUEST_URI'] (see default_options for the list of available options); if several regexps match the $_SERVER['REQUEST_URI'], only the last one will be used
memorize_headers Array array() an array of strings corresponding to some HTTP headers name. Listed headers will be stored with cache datas and "replayed" when the cache is hit

Examples

Use of Zend_Cache_Frontend_Page is really trivial:

<?php
// [...] // require, configuration and factory

$cache->start();
// if the cache is hit, the result is sent to the browser
// and the script stop here

// rest of the page ...

a more complex example which shows a way to get a centralized cache management in a bootstrap file (for using with Zend_Controller for example)

<?php
/*
 * You should avoid putting too many lines before the cache section.
 * For example, for optimal performances, "require_once" or
 * "Zend_Loader::loadClass" should be after the cache section.
 */

$frontendOptions = array(
   
'lifetime' => 7200,
   
'debug_header' => true// for debugging
   
'regexps' => array(
       
// cache the whole IndexController
       
'^/$' => array('cache' => true),

       
// cache the whole IndexController
       
'^/index/' => array('cache' => true),

       
// we don't cache the ArticleController...
       
'^/article/' => array('cache' => false),

       
// ... but we cache the "view" action of this ArticleController
       
'^/article/view/' => array(
           
'cache' => true,

           
// and we cache even there are some variables in $_POST
           
'cache_with_post_variables' => true,

           
// but the cache will be dependent on the $_POST array
           
'make_id_with_post_variables' => true
       
)
   )
);

$backendOptions = array(
    
'cache_dir' => '/tmp/'
);

// getting a Zend_Cache_Frontend_Page object
$cache Zend_Cache::factory('Page',
                             
'File',
                             
$frontendOptions,
                             
$backendOptions);

$cache->start();
// if the cache is hit, the result is sent to the browser and the
// script stop here

// [...] the end of the bootstrap file
// these lines won't be executed if the cache is hit

The Specific Cancel Method

Because of design issues, in some cases (for example when using non HTTP 200 return codes), you could need to cancel the current cache process. So we introduce for this particular frontend, the cancel() method.

<?php
// [...] // require, configuration and factory

$cache->start();

// [...]

if ($someTest) {
    
$cache->cancel();
    
// [...]
}

// [...]

Zend Framework