Improve performances of any web site: cache pages with Zend Framework

Zend Framework is decoupled, so it’s possible to use only the needed features.

I had to improve the performances of an old site, with no cache at all. Changing all the database/file_get_contents requests would have been a long process, so I decided to enable the cache of the web pages with Zend Framework  FrontEnd Page Cache using only few lines of code at the beginning of each page.
That web site uses a header file so I placed there the code only once.

In case there are multiple files, a solution (not tested) could be placing this code in a separate file, then prepend it to all the script thanks to a .htaccees directive:

php_value auto_prepend_file "page_cache.php"

My working example (for my purposes, pages cached if there are variables in $_GET and $_COOKIE and the cache id depends on them, 2 level directories cache, 2h expires time):

// place before any output is started in the "header" script
require_once 'Zend/Cache.php';

$cache = Zend_Cache::factory(
      'lifetime'		=> 7200,
      'ignore_user_abort'	=> true,
      'default_options' => array (
          'cache_with_get_variables'	=> true,
          'cache_with_cookie_variables'	=> true,
          'make_id_with_get_variables'	=> true,
          'make_id_with_cookie_variables' => true,
       'cache_dir'			=> './cache',
	'hashed_directory_level' 	=> 2

// ... site content...

5 thoughts on “Improve performances of any web site: cache pages with Zend Framework

  1. Pingback: Optimization of Website for Yahoo and Google is Challenge for SEO »

  2. Pingback: Optimising Zend Framework applications – part 2

  3. @Stephen

    On all the application I’ve worked, I found that the caching should be done at two levels:
    – slow sources (db, remote sources like REST requests)
    – page cache (pre boostrap with zend, or apache level with reverse proxies / nginx)

    I do not see advantages in act in intermediate position (including the controller level)

Comments are closed.