Search code examples
zend-frameworkzend-cache

Zend_Cache caches the entire site instead of a controller


I wanted to cache controller ajax

<?php
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
    public function _initCache()
    {
        $frontendOptions = array(
            'lifetime' => 10800,
            'automatic_serialization' => true,
            'debug_header' => false,
            'regexps' => array('^/ajax/' => array('cache' => true),
                               '^/admin/' => array('cache' => false)),
            'default_options' => array(
            'cache_with_cookie_variables' => true,
            'make_id_with_cookie_variables' => false));

        $backendOptions = array('cache_dir' => APPLICATION_PATH.'/data/cache');
        $cache = Zend_Cache::factory('Page','File',$frontendOptions,$backendOptions);

        $cache->start();
    }
}

But caching the entire site, including the admin module.


Solution

  • Use debug_header to true and check , the code below. As we set not to cache all pages at first, and only to cache the page starting with ajax , I hope all the ajax pages starts with the name ajax else change accordingly the regexp.

    class Default_Bootstrap extends Zend_Application_Module_Bootstrap
    {
        public function _initCache()
        {
            $frontendOptions = array(
                'lifetime' => 10800,
                'automatic_serialization' => true,
                'debug_header' => true,                
                'regexps' => array(
                    '$' => array('cache' => false),
                    '/ajax' => array('cache' => true),
                ),
                'default_options' => array(
                    'cache_with_cookie_variables' => true,
                    'make_id_with_cookie_variables' => false
                )
            );
    
            $backendOptions = array('cache_dir' => APPLICATION_PATH.'/data/cache');
            $cache = Zend_Cache::factory('Page','File',$frontendOptions,$backendOptions);
    
            $cache->start();
        }
    }