Setting Up Global Cache in Zend Framework

submit to reddit

In a large scale web application, especially based on Zend Framework, there are lot’s of components that support built in cache support. Such are Zend_Db, Zend_Translate, Zend_Date, etc. Also you may need cache support wherever in the app, so my advice is to setup a cache instance in the “beginning”, into the bootstrap.php or even better – into a front controller plugin, and to store it into the Zend_Registry. Thus you’ve to change only the lifetime for specific needs:

<?php
 
class CacheInit extends Zend_Controller_Plugin_Abstract
{
    public function __construct()
    {
        $frontendOptions = array(
            'automatic_serialization' => true,
            'lifetime' => 60
        );
 
        $backendOptions  = array(
            'cache_dir' => realpath(APPLICATION_PATH . '/../cache')
        );
 
        $cache = Zend_Cache::factory('Core',
                                     'File',
                                     $frontendOptions,
                                     $backendOptions);
 
        Zend_Registry::set('cache', $cache);
    }
}

Than add it as a front controller plugin:

// cache plugin
$frontController->registerPlugin(new CacheInit());

Related posts:

  1. Zend Framework: Cache Database Table Schemes
  2. Setting Up Zend Framework with Modules
  3. Theory of caching. Zend_Cache & Zend Optimizer.

You are a GREAT developer? Click here to answer the weekly quiz!

This entry was posted in micro tutorial, PHP, zend framework and tagged , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Setting Up Global Cache in Zend Framework

  1. Ryan Mauger says:

    OR, you could use the cache manager plugin resource, and configure multiple caches with your config, rather than hard code a single one in a plugin.

    http://www.framework.zend.com/manual/en/zend.application.available-resources.html

  2. reza says:

    hi , that’s really interesting and cool way to firing up zend cache. thank you so much .

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">