学习练习 zend framework ?php/*zend_cache 文件缓存的基本操作*/require_once(zend/loader.php);//载入zend缓存类(zend_cache)zend_loader::loadclass(zend_cache);//前端缓存设置(生命周期、是否序列化)$foptions = array('lifetime' = 60 , 'automtic_
学习练习
zend framework 60 , 'automtic_serialization' => true);//后端缓存设置(缓存存放路径)$boptions = array('cachedir' => 'cache');//开启缓存模式,(core[核心],file[文件],前端缓存配置信息,后端缓存配置信息)$cache = zend_cache::factory('core','file',$foptions,$boptions);//判断缓存是否存在,如果存在则载入缓存load('string'[缓存名称])if ($result = $cache -> load('cache_two')) { echo 缓存已经存在!
; print_r($result);}else{ //如果缓存不存在则读取文件,并将文件内容写入湖缓存 echo 缓存不存在!
; $filename = 'temp.txt'; $fopen = fopen($filename,'r'); $result = fread($fopen, filesize($filename)); fclose($fopen); //保存缓存方式load($result[读取资源],'缓存名称') $cache -> save($result,'cache_two'); print_r($result);}?>
'127.0.0.1' , 'username' => 'root' , 'password' => '111' , 'dbname' => 'test', 'profiler' => true );$db = zend_db::factory('pdo_mysql',$config);$table = 'sanguo';$select = $db -> select();$select -> from($table,'*');$select -> where(s_sheng='河北');$result = $db -> fetchall($select);//-----------------end------------////---将数据库读取的资源以output方式写入缓存---//$foptions = array('lifetime' => 3600 , 'automtic_serialization' => true);$boptions = array('cachedir' => 'cache');$cache = zend_cache::factory('output','file',$foptions,$boptions);if (!$cache -> start('hebei10')) { echo ; foreach ($result as $key => $value) { echo ; foreach ($value as $key2 => $value2) { echo . $value2 . ; } echo
; } echo
; echo 这里是缓存时间: . date(y-m-d h:i:s) .
; $cache -> end();}//-----用load()方法调用并输出缓存------//echo 这里没有被缓存: . date(y-m-d h:i:s) .
;$result2 = $cache -> load('hebei10');echo $result2;?>
3600 , 'automtic_serialization' => true);$boptions = array('cachedir' => 'cache');$cache = zend_cache::factory('function','file',$foptions,$boptions);function gongbeishu($num1,$num2){ for ($i=$num1; $i call('gongbeishu',array(1,50),array('temp_gongbeishu'));$date = $cache ->call('gongbeishu',array(1,50),array('temp_gongbeishu'));print_r($date);?>
new test() 可以调用任何权限方法or'cached_entity' => 'test' 只能调用静态方法*/$foptions = array('cached_entity' => new test());//开启缓存(前端,后端,前端配置)$cache = zend_cache::factory('class','file',$foptions);//第一次调用被写入缓存,第二次调用才会返回;$cache -> sum(9,9);$cache -> cheng(9,9);echo sum()方法返回结果为: . $cache -> sum(3,3) .
;echo cheng()方法返回结果为: . $cache -> cheng(2,7) .
;?>
$cache = zend_cache::factory('core','file',$foptions,$boptions);if ($result = $cache -> load('cache_two')) { echo 缓存已经存在!
; print_r($result); $cache -> clean('all'); echo
; echo 缓存已经清除!
;}