Zend Framework 1 - Zend Caching

362 views Asked by At

I am working on optimizing Zend Framework Application with Doctrine ORM. I can't figure it out what particular code would I use in my controller to get this caching. Whenever I pass again the same url it should use the cache code instead of processing that logic again.

My Bootstrap file for cache looks like this:-

protected function _initCache() { 

$frontendOptions = array(                     
    'lifetime' => 7200, 'content_type_memorization' => true, 
    'default_options' => array( 
        'cache'                        => true, 
        'cache_with_get_variables'     => true,
        'cache_with_post_variables'    => true, 
        'cache_with_session_variables' => true, 
        'cache_with_cookie_variables'  => true, ), 
        'regexps' => array( 
            // cache the whole IndexController
            '^/.*'     => array('cache' => true), 
            '^/index/' => array('cache' => true),
            // place more controller links here to cache them 
        )
    ); 

$backendOptions = array(
    'cache_dir' => APPLICATION_PATH ."/../cache" // Directory where to put the cache files
);

$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions); 
$cache->start(); 
Zend_Registry::set("cache", $cache);
} 

Any help would be appreciated.

1

There are 1 answers

0
vivek s vamja On

Check this below code to set cache if not exist or get cache if exists.

$result =””;
$cache = Zend_Registry::get('cache');

if(!$result = $cache->load('mydata')) {
    echo 'caching the data…..';
    $data=array(1,2,3); // demo data which you want to store in cache
    $cache->save($data, 'mydata');
} else {
    echo 'retrieving cache data…….';
    Zend_Debug::dump($result);
}