Zend Cache with Zend Framework (Simple Function)
by Root on Feb.24, 2009, under Php, programlama, zend
Ok. In my new project we have a lot of dymanic part. This part actually not up-to-date every time. (maybe one or two times in a day). If we use html for this part , we are so tried to update it. If we use sql all time (daily 2k+ visitor and growing) system to slow. And hero is come Zend Cache.
Zend Cache is very good for our job. We can use for it some part to cache. And other parts is dynamic.
This is our site (Plan
) // Ok i know this is table
. But system is not.
| No Cache Needed (logo) | ||
| Cache for 2 hours | Cache for 10 minutes | No Cache Needed |
| Cache for 1 day | Cache for 1 Day | |
| No Cache Needed | Cache for 10 minutes | No Cache Needed |
| No Cache Needed (down link,address,telephones) | ||
Ok. There is very important. More user means more sql and more sql means more slow.
And answer is Zend: Here my function for zend cache.
Test on 1.7 framework.
Important PS: In my project (pls look) every work evalute in indexController class. You can write any where you needed.
Insert this code on your code. (i put this code indexController )
[PHP]
public function cacheOnRoot($id,$content,$lifetime=30)
{
$frontendOptions = array(
‘lifetime’ => $lifetime,
‘automatic_serialization’ => false
);
$backendOptions = array(‘cache_dir’ => ‘./tmp/’);
$cache = Zend_Cache::factory(‘Page’,
‘File’,
$frontendOptions,
$backendOptions);
if (!($cache->test($id))) {
$content=$content.”<br />”.date(“d-m-Y H:i:s”);
echo $content;
$cache->save($content);
} else {
$content = $cache->load($id);
}
return $content;
}
[PHP]
USE:
public function getSectorsMainMenu()
{
$sectors_menu=$this->view->partial(‘sectors.phtml’);
/* Gets content in sectors.phtml Note: this is not dynamic for now. (In this function you can write it dynamic) */
$sectors_menu=$this->cacheOnRoot(“sectormenu”,$sectors_menu,120);
return $sectors_menu;
}
echo $this->getSectorsMainMenu();