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

PHP數(shù)據(jù)緩存技術(shù)

 更新時(shí)間:2007年02月14日 00:00:00   作者:  
數(shù)據(jù)緩存是web開發(fā)中常用的一種性能優(yōu)化方法。目前主要文件緩存或者數(shù)據(jù)庫緩存兩種形式,數(shù)據(jù)庫緩存數(shù)據(jù)庫不是什么不可能的事情,的確也是很好很重要的。我認(rèn)為傳統(tǒng)數(shù)據(jù)庫主要是從業(yè)務(wù)層、模塊設(shè)計(jì)等方面來考慮的,而緩存數(shù)據(jù)庫主要是從實(shí)現(xiàn)層來設(shè)計(jì)的,主要是為了緩存常用的多表查詢之類的。這里主要將的是文件緩存,網(wǎng)上很多資料了,這里我轉(zhuǎn)載了一些原理資料。
   Cache是“以空間換時(shí)間”策略的典型應(yīng)用模式,是提高系統(tǒng)性能的一種重要方法。緩存的使用在大訪問量的情況下能夠極大的減少對(duì)數(shù)據(jù)庫操作的次數(shù),明顯降低系統(tǒng)負(fù)荷提高系統(tǒng)性能。相比頁面的緩存,結(jié)果集是一種“原始數(shù)據(jù)”不包含格式信息,數(shù)據(jù)量相對(duì)較小,而且可以再進(jìn)行格式化,所以顯得相當(dāng)靈活。由于PHP是“一邊編譯一邊執(zhí)行”的腳本語言,某種程度上也提供了一種相當(dāng)方便的結(jié)果集緩存使用方法——通過動(dòng)態(tài)include相應(yīng)的數(shù)據(jù)定義代碼段的方式使用緩存。如果在“RamDisk”上建緩存的話,效率應(yīng)該還可以得到進(jìn)一步的提升。以下是一小段示例代碼,供參考。

<?
// load data with cache 

function load_data($id,$cache_lifetime) { 

// the return data 

$data = array(); 

// make cache filename 

$cache_filename ‘cache_‘.$id..php‘

// check cache file‘s last modify time 

$cache_filetime filemtime($cache_filename); 

if (
time() - $cache_filetime <= $cache_lifetime) { 

//** the cache is not expire 

include($cache_filename); 

} else { 

//** the cache is expired 

// load data from database 

// ... 

while ($dbo->nextRecord()) { 

// $data[] = ... 



// format the data as a php file 

$data_cache "

while (list($key, $val) = each($data)) { 

$data_cache .= "
$data[‘$key‘]=array("; 

$data_cache .= "
‘NAME‘=>"".qoute($val[‘NAME‘])."\"," 

$data_cache .= "‘VALUE‘=>\"".qoute($val[‘VALUE‘])."\"" 

$data_cache .= ";);\r\n"



$data_cache "?>\r\n"

// save the data to the cache file 

if ($fd fopen($cache_filename,‘w+)) { 

fputs($fd,$data_cache); 

fclose($fd); 





return 
$data



?> 


適用情況:
1.數(shù)據(jù)相對(duì)比較穩(wěn)定,主要是讀取操作。
2.文件操作要比數(shù)據(jù)庫操作快。
3.復(fù)雜數(shù)據(jù)訪問,大數(shù)據(jù)量訪問,密集數(shù)據(jù)訪問,系統(tǒng)數(shù)據(jù)庫負(fù)載極重。
4.Web/DB分離結(jié)構(gòu)或者多Web單DB結(jié)構(gòu)。

未經(jīng)證實(shí)的問題:
1.并發(fā)訪問時(shí)對(duì)文件的讀寫是否會(huì)引起鎖定問題。
2.涉及到的數(shù)據(jù)文件太多時(shí),性能如何。
擴(kuò)展思路:
1.生成JavaScript數(shù)據(jù)定義代碼,在客戶端調(diào)用。
2.還未想到……  

望共同探討。

相關(guān)文章

最新評(píng)論