Zend Routes Page Cache Configured with Application.ini

I recently started a project utilizing Zend_Cache_Frontend_Page to cache the complete page output by our routes.

The Zend_Application functionality in the Zend Framework allows you to easily configure route mapping in your application.ini but does not provide the same integration for caching.

One of the annoyances with Zend_Cache_Frontend_Page is that you have to define regular expressions and options for each of the pages you want to cache.

Since the routes are already defined in the application.ini file, I modified the routes with some extra options to define caching parameters...

; Routes 
resources.router.routes.home.type = "Zend_Controller_Router_Route_Regex" 
resources.router.routes.home.route = "home\?p=(\d)" 
resources.router.routes.home.defaults.controller = "members" 
resources.router.routes.home.defaults.action = "index" 
resources.router.routes.home.map.1 = "p" 
resources.router.routes.home.reverse = "home?p=%d" 
resources.router.routes.home.cache.lifetime = 3600 
resources.router.routes.home.cache.idwithget = true 
resources.router.routes.home.cache.idwithpost = true 
resources.router.routes.home.cache.idwithsession = true

Then modified my application bootstrap class to handle these options...

public function _initCachePage() { 
  $resources = $this->_options['resources']; 
  $cacheRoutesRegex = array(); 
  foreach($resources['router']['routes'] as $key=>$route) { 
    if($route['cache']['lifetime'] > 0) { 
      $cacheRoutesRegex["{$route['route']}"] = array( 'cache' => true , 'specific_lifetime' => $route['cache']['lifetime'] ); 
      if($route['cache']['idwithget']) $cacheRoutesRegex["{$route['route']}"]['make_id_with_get_variables'] = true; 
      if($route['cache']['idwithpost']) $cacheRoutesRegex["{$route['route']}"]['make_id_with_post_variables'] = true; 
      if($route['cache']['idwithsession']) $cacheRoutesRegex["{$route['route']}"]['make_id_with_session_variables'] = true; 
    } 
  } 
  // Page Caching Frontend 
  $frontendOptions = array( 
    'default_options' => array( 
      'cache' => false , 
      'cache_with_post_variables' => true , 
      'cache_with_get_variables' => true , 
      'cache_with_cookie_variables' => true 
    ), 
    'debug_header' => true // Turn off for production , 
    'regexps' => $cacheRoutesRegex 
  ); 
  // File Based Backend 
  $backendOptions = array( 
    'cache_dir' => APPLICATION_PATH . '/../data/cache' 
  ); 
  $cachePage = Zend_Cache::factory( 'Page' , 'File' , $frontendOptions , $backendOptions ); $cachePage->start(); 
}

Now I can define my routes and page caching parameters in my application configuration files.