欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP中常見的緩存技術實例分析

 更新時間:2015年09月23日 12:18:44   作者:wolly  
這篇文章主要介紹了PHP中常見的緩存技術,以實例形式較為詳細的分析了php中緩存技術的原理、特點及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例分析了PHP中常見的緩存技術。分享給大家供大家參考。具體如下:

JBLOG在開發(fā)的過程中,對性能的優(yōu)化做了不少工作。為了盡量減少不必要的數(shù)據(jù)庫查詢,我對一些數(shù)據(jù)進行了緩存和靜態(tài)化處理。

緩存的原理:把一些經(jīng)常要用到但又很少改動的數(shù)據(jù)以數(shù)組或其它形式存儲到一個獨立的PHP文件中,然后在需要用到的時候包含進來。

緩存的優(yōu)點:能夠大大減少數(shù)據(jù)庫的查詢次數(shù),減輕數(shù)據(jù)庫的壓力,提高程序的執(zhí)行效率。

JBLOG緩存的數(shù)據(jù)有:系統(tǒng)設置、博客分類、側欄最新日志、最新評論、博客統(tǒng)計、日志歸檔、友情鏈接、標簽等。通過緩存這些數(shù)據(jù),執(zhí)行一次頁面數(shù)據(jù)庫的查詢次數(shù)從十幾次減少到3次。

JBLOG中與緩存相關的函數(shù)保存在include目錄下的cache.func.php里,主要函數(shù):

//刷新緩存
function recache($cachestr = '') {
if (!$cachestr) {
 $cachelist = array('config','class','archive','newcomment','newpost','link','tag','statistic','topblog');
} else {
 $cachelist = explode(',',$cachestr);
 foreach ($cachelist as $cache) {
  $cachename = $cache.'_recache';
  if (function_exists($cachename)) {
  $cachename();
  }
 }
}
}

recache()函數(shù)用來刷新緩存,每一個緩存以獨立的函數(shù)存在,刷新緩存時僅需執(zhí)行一次相應的函數(shù)即可。

//將字符串寫進文件
function writeToFile($cachename,$content = '') {
$allowcache = $cachelist = array('config','class','archive','newcomment','newpost','link','tag','statistic','topblog');
if (in_array($cachename,$allowcache)) {
 $cache_dir = JBLOG_ROOT.'cache_data/';
 $cache_file_name = $cache_dir.'cache_'.$cachename.'.php';
 if (!is_dir($cache_dir)) {
  @mkdir($cache_dir,0777);
 }
 if ($fp = @fopen($cache_file_name,'wb')) {
  $content = "<?php\r\n//該文件是系統(tǒng)自動生成的緩存文件,請勿修改\r\n//創(chuàng)建時間:".date('Y-m-d H:i:s',time())."\r\n\r\nif (!defined('IN_JBLOG')) {exit('Access Denied!');}\r\n\r\n".$content."\r\n\r\n?>";
  @fwrite($fp,$content);
  @fclose();
  @chmod($cache_file_name,0777);
 } else {
  echo '緩存文件<b>'.$cache_dir.$cache_file_name.'</b>創(chuàng)建失??!<br />';
 }
} else {
 die('緩存名稱<b>'.$cachename.'</b>不在系統(tǒng)允許的范圍內(nèi)!');
}
}

writeToFile() 函數(shù)用于將數(shù)據(jù)寫入到緩存目錄下以cache_緩存名稱.php命名的文件中。
再看具體的緩存函數(shù)實例:

//緩存博客分類
function class_recache() {
global $db,$tablepre;
$content = '';
$sql = "SELECT id,classname,description,orderid,arcnum FROM `{$tablepre}class` ORDER BY orderid";
$result = $db->query($sql);
while ($row = $db->fetch_array($result)) {
 $content .= "\tarray(\r\n";
 $content .= "\t'id'=>'".addslashes($row['id'])."',\r\n";
 $content .= "\t'classname'=>'".addslashes($row['classname'])."',\r\n";
 $content .= "\t'description'=>'".addslashes($row['description'])."',\r\n";
 $content .= "\t'orderid'=>'".addslashes($row['orderid'])."',\r\n";
 $content .= "\t'arcnum'=>'".addslashes($row['arcnum'])."',\r\n";
 $content .= "\t),\r\n";
}
$content = substr($content,0,strrpos($content,','));
$content = "\$class_cache = array(\r\n{$content}\r\n);";
writeToFile('class',$content);
}

class_recache()函數(shù)從數(shù)據(jù)庫取出數(shù)據(jù),然后構造一個數(shù)組,以分類ID作為數(shù)組的索引,類別的信息作為對應的值,方便數(shù)據(jù)的訪問。
緩存的引入:

所有緩存數(shù)據(jù)統(tǒng)一在include目錄下的common.inc.php引入,代碼如下:

//加載系統(tǒng)設置信息,文件不存在則重建緩存
if ([email=!@include(JBLOG_ROOT.]!@include(JBLOG_ROOT.'cache_data/cache_config.php'[/email])) {
require_once(JBLOG_ROOT.'include/cache.func.php');
recache('config');
exit('成功創(chuàng)建系統(tǒng)配置信息緩存,請刷新頁面!');
}
//加載緩存,緩存文件不存在則重建緩存
$cachestr = '';
$cachelist = array('class','archive','newcomment','newpost','link','tag','statistic','topblog');
foreach ($cachelist as $cachename) {
$cachestr .= (@include(JBLOG_ROOT.'cache_data/cache_'.$cachename.'.php')) ? '' : $cachename.',';
}
$cachestr = substr($cachestr,0,strrpos($cachestr,','));
if ($cachestr) {
require_once(JBLOG_ROOT.'include/cache.func.php');
recache($cachestr);
exit('所有緩存重建完成,請刷新頁面!');
}
unset($cachelist,$cachename,$cachestr);

先加載配置信息是因為,在創(chuàng)建其它緩存文件的時候,經(jīng)常要用到系統(tǒng)的設置信息,如系統(tǒng)設置中有一個選項可以讓用戶自定義最新日志的數(shù)量,在緩存最新日志的時候就會用到該變量,所以必須先確保配置信息成功緩存后,再緩存其它項目。

希望本文所述對大家的php程序設計有所幫助。

相關文章

最新評論