学习练习 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 "| ". $value2 . " | "; } echo "
"; $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 <= $num2 ; $i++)
{
if ($i%3 == 0)
{
$Result[] = $i;
}
}
return $Result;
}
$Cache -> 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 "缓存已经清除!
";
}









