ZF框架Zend_Cache 缓存基本使用方法
学习练习
学习练习
<?php
/*Zend_Cache 文件缓存的基本操作*/
require_once("Zend/Loader.php");
//载入Zend缓存类(Zend_Cache)
Zend_Loader::loadClass("Zend_Cache");
//前端缓存设置(生命周期、是否序列化)
$Foptions = array('lifetime' => 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 "缓存已经存在!<br>";
print_r($Result);
}
else
{
//如果缓存不存在则读取文件,并将文件内容写入湖缓存
echo "缓存不存在!<br>";
$Filename = 'temp.txt';
$Fopen = fopen($Filename,'r');
$Result = fread($Fopen, filesize($Filename));
fclose($Fopen);
//保存缓存方式load($Result[读取资源],'缓存名称')
$Cache -> save($Result,'cache_two');
print_r($Result);
}
?>
2. [代码]基本前端用法(Output) 跳至 [1] [2] [3] [4] [5] [全屏预览]
<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass("Zend_Cache");
Zend_Loader::loadClass("Zend_Db");
//------------数据库连接配置------------//
$Config = array('host' => '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 "<table border='1' width='500'>";
foreach ($Result as $key => $value)
{
echo "<tr>";
foreach ($value as $key2 => $value2)
{
echo "<td>". $value2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "这里是缓存时间:" . date("Y-m-d H:i:s") ."<br>";
$Cache -> end();
}
//-----用load()方法调用并输出缓存------//
echo "<hr>这里没有被缓存:" . date("Y-m-d H:i:s") . "<br>";
$Result2 = $Cache -> load('hebei10');
echo $Result2;
?>
3. [代码]基本前端用法(Function) 跳至 [1] [2] [3] [4] [5] [全屏预览]
<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass('Zend_Cache');
$Foptions = array('lifetime' => 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);
?>
4. [代码]基本前端用法(Class) 跳至 [1] [2] [3] [4] [5] [全屏预览]
<?php
/*ZF缓存类前端Class的使用*/
require_once("Zend/Loader.php");
Zend_Loader::loadClass('Zend_Cache');
//自定义一个测试用的类
class test
{
public static function sum($num1,$num2)
{
return ($num1 + $num2);
}
public function cheng($num1,$num2)
{
return ($num1 * $num2);
}
}
//前端配置:cached_entity所对应的值为类名
/*注:如果类中存在非静态方法,赋值时必须实例化,
否则在调用时只能调用静态方法 例如:
'cached_entity' => 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) . "<br>";
echo "cheng()方法返回结果为:" . $Cache -> cheng(2,7) . "<br>";
?>
5. [代码]clean() 清除缓存 跳至 [1] [2] [3] [4] [5] [全屏预览]
$Cache = Zend_Cache::factory('Core','File',$Foptions,$Boptions);
if ($Result = $Cache -> load('cache_two'))
{
echo "缓存已经存在!<br>";
print_r($Result);
$Cache -> clean('all');
echo "<br>";
echo "缓存已经清除!<br>";
}
精彩图集
精彩文章






