如下图所示建立工程: 代码如下: <?PHP /** *IndexController-Thedefaultcontrollerclass * *@author *@version */ require_once'Zend/Controller/Action.php'; require_once'Zend/Cache.php'; require_once'Zend/ReGIStry.php'; require_once'Zend/Db.php'; require_once'Zend/Db/Table.php';; classIndexControllerextendsZend_Controller_Action { publicfunctioninit() { $params=array('host'=>'localhost', 'username'=>'root', 'password'=>'root', 'dbname' =>'MySQL'); $db=Zend_Db::factory('Pdo_Mysql',$params); Zend_Db_Table::setDefaultAdapter($db); Zend_Registry::set('db',$db); } publicfunctionindexAction() { $frontendOptions=array( 'lifeTime'=>7200,//两小时的缓存生命期 'automatic_serialization'=>true ); $backendOptions=array( 'cache_dir'=>'C:/tmp/'//放缓存文件的目录 ); //取得一个Zend_Cache_Core对象 $cache=Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); if(!$result=$cache->load('myresult')){ //缓存不命中;连接到数据库 $dbAdapter=Zend_Registry::get('db'); $result=$dbAdapter->query('SELECT*FROMuser')->fetchAll(); $cache->save($result,'myresult'); echo"Thisoneisfromdatabase!<br/>"; }else{ //cachehit!shoutsothatweknow echo"Thisoneisfromcache!<br/>"; } print_r($result); } }
(责任编辑:admin) |