《PHP實戰(zhàn):常見php數(shù)據(jù)文件緩存類匯總》要點:
本文介紹了PHP實戰(zhàn):常見php數(shù)據(jù)文件緩存類匯總,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應用本文實例匯總了常見php數(shù)據(jù)文件緩存類.分享給大家供大家參考.具體分析如下:
PHP應用數(shù)據(jù)文件緩存的做法我們常用的有php文件緩存與利用memcache來緩存數(shù)據(jù),下面面我分別總結(jié)了memcache緩存數(shù)據(jù)與數(shù)據(jù)文件緩存.感興趣的朋友可以參考一下.
PHP應用1.對于一般的變量,把該變量變成php語言的格式,寫到文件中,用時只要include這個文件就相當于加載了cache了.
PHP應用2.對于array型的變量,把array轉(zhuǎn)化為php語言定義array的字符串,寫到文件中,用時也只要include就相當于加載了cache了.
PHP應用3.緩存cache時間上的控制,通過獲取緩存文件的創(chuàng)建時間和現(xiàn)在的時間進行對比,如果沒有到更新時間,直接讀取緩存,如果到了更新時間,查詢數(shù)據(jù)庫.
PHP應用文件緩存類,代碼如下:
代碼如下:
<?php
class DataCache
{
?/**
? * 數(shù)組轉(zhuǎn)換
? *
? * @param array $array
? * @param string $arrayName
? * @param array $level
? *
? * @return string
? */
?private function arrayEval($array, $arrayName = '', $level = 0)
?{
? $space = str_repeat("t", $level);
?
? if (emptyempty($arrayName))
? {
?? $evaluate = "arrayn$space(n";
? }
? else
? {
?? $evaluate = "${$arrayName} = arrayn$space(n";
? }
?
? $space2 = str_repeat("t", $level + 1);
? $comma = $space2;
? if (!emptyempty($array))
? {
?? foreach ($array as $key => $val)
?? {
??? $key = is_string($key) ? ''' . addcslashes($key, ''\') . ''' : $key;
??? $val = !is_array($val) && (!preg_match('/^-?[1-9]d*$/', $val) || strlen($val) > 12) ? ''' . addcslashes($val, ''\') . ''' : $val;
??? if (is_array($val))
??? {
???? $evaluate .= "$comma$key => " . arrayEval($val, '', $level + 1);
??? }
??? else
??? {
???? $evaluate .= "$comma$key => $val";
??? }
??? $comma = ",n$space2";
?? }
? }
? $evaluate .= "n$space)";
?
? // 最后才需要一個“;”
? if ($level == 0)
? {
?? $evaluate .= ";";
? }
? return $evaluate;
?}
?
?/**
? * 寫入緩存
? *
? * @param string $path
? * @param string $arrayName
? * @param array? $data
? *
? * @return boolean
? */
?public static function writeCache($path, $arrayName, $data)
?{
? if ($handle = fopen($path, 'w+'))
? {
?? $data = self::arrayEval($data, $arrayName);
?
?? $dataConvert = "<?phpn" . $data;
?
?? flock($handle, LOCK_EX);
?? $rs = fputs($handle, $dataConvert);
?? flock($handle, LOCK_UN);
?? fclose($handle);
?? if ($rs !== false)
?? {
??? return true;
?? }
? }
? return false;
?}
}
?>
調(diào)用辦法,代碼如下:
代碼如下:
/**
* 生成文件緩存
*
* @param string $filePath 緩存文件的保存路徑
* @param string $arrayName 存放在緩存文件中的數(shù)組名稱
* @param array $data 數(shù)據(jù)
*
* @return boolean
*/
DataCache::writeCache($filePath, $arrayName, $data);
memcache來緩存數(shù)據(jù),這里提供這個文件緩存的類,代碼如下:
代碼如下:
<?php
/**
?* 文件緩存類
?* 提供文件緩存
?*/
class Cache_FileCache{
????
??? /**
???? * 設置緩存
???? * @param $key 緩存的關鍵字key
???? * @param $data 緩存的內(nèi)容
???? * @param $cacheLife 緩存時間(單位為秒)如果為0 則表示無限時間
???? * @return Bool
???? */
??? public static function setCache($key,$data,$cacheLife)
??? {
??????????? if(file_exists(__SITE_FILE_CACHE))
??????????? {
??????????????? @$file??????????????? =? __SITE_FILE_CACHE . "/" . $key .".php";
??????????????? $cache????????????????? =? array();
??????????????? $time??????????????? =? __SYS_TIME;
??????????????? $cache['content']??? =? $data;
??????????????? $cache['expire']??? =? $cacheLife === 0 ? 0 : $time+$cacheLife;
??????????????? $cache['mtime']??????? =? $time;
??????????????? $cache??????????????? =? serialize($cache);
??????????????? $setReslut??????????? =? @file_put_contents($file,$cache) or self::error(__line__,"文件寫入出錯");
??????????????? $chmodReslut??????? =? @chmod($file,0777) or self::error(__line__,"設定文件權(quán)限失敗");
??????????????? if($setReslut && $chmodReslut)
??????????????? {
??????????????????? return true;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return false;
??????????????? }
??????????? }
??? }
?
??? /**
???? * 得到緩存數(shù)據(jù)
???? * @param $key 緩存的關鍵字key
???? * @return array
???? */
??? public static function getCache($key)
??? {
??????????? @$file??????????????? =????? __SITE_FILE_CACHE . "/" . $key .".php";
??????????? if(file_exists($file))
??????????? {
???????????????????? $data??????? =??? @file_get_contents($file);
???????????????????? $data??????? =?? unserialize($data);
???????????????????? if($data['expire']==0 || $data['expire'] > __SYS_TIME)
???????????????????? {
???????????????????????? return $data['content'];
???????????????????? }
???????????????????? else?
???????????????????? {
???????????????????????? unlink($file);
???????????????????????? return array();
???????????????????? }
??????????? }????????
??? }
????
??? /**
???? * 刪除緩存文件
???? * @param $key 緩存$key
???? * @return Bool
???? */
??? public static function delCache($key)
??? {????????
??????? if (@unlink(__SITE_FILE_CACHE."/".$key.".php"))
??????? {
??????????? return true;
??????? }
??????? else
??????? {
??????????? return false;
??????? }
??? }
????
??? /**
???? * 清除所有緩存文件
???? * @return Bool
???? */
????
??? public static function clearAllCache()
??? {
??????? $files = scandir(__SITE_FILE_CACHE);
??????? foreach ($files as $val)
??????? {
??????????? @unlink(__SITE_FILE_CACHE."/".$val);
??????? }
??? }
????
??? /**
???? * 出錯處理函數(shù)
???? * @param $line 行數(shù)
???? * @param $msg? 信息
???? */
??? public static function error($line,$msg)
??? {
??????? die("出錯文件:".__file__."/n出錯行:$line/n錯誤信息:$msg");
??? }
}
?>
PHP應用希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助.
《PHP實戰(zhàn):常見php數(shù)據(jù)文件緩存類匯總》是否對您有啟發(fā),歡迎查看更多與《PHP實戰(zhàn):常見php數(shù)據(jù)文件緩存類匯總》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/13452.html