Thứ Bảy, 3 tháng 3, 2012

Cache trong php - c�ch vi?t code cache trong php

 M?t trang php co b?n th�ng thu?ng kh�gn c?n cache nhung n?u m?t trang c� luu lu?ng truy c?p l?n ho?c m?t CMS m� ngu?n n�o cung ?ng d?ng cache , sau d�y m�nh s? ch? cho c�c b?n c�ch cache don gi?n nh?t gi�p tang kh? nang c?a server v� tang t?c trang c?a b?n.

Client s? g?i request t?i server, thu?ng th� ch? l� m?t url, l�n t?i bu?c n�y th� kh�ng t?n th?i gian. Ti?p d� server s? parse request, g?i t?i DB, v� l?y d? li?u t? DB tr? v?, parse d? li?u v� tr? l?i cho Browser, vi?c query t?i DB s? t?n r?t nhi?u th?i gian, th�m n?a d? li?u response thu?ng l?n, d�y ch�nh l� nguy�n nh�n l�m cho ?ng d?ng c?a ta ch?m di.

T?o cache s? gi�p cho site ch?y nhanh v� gi?m truy c?p tr?c ti?p v�o database, v?i php vi?c t?o cache ch? ? m?c genarate ra file, thu?ng l� html. ��y l� gi?i ph�p r?t hay n?u l�m t?t, c� th? tang t?c d? lu?t web l�n h�ng ch?c l?n. Tu? v�o m?c d�ch c?a ?ng d?ng d? cache, cung nhu k?t h?p c�c phuong ph�p cho hi?u qu?:

1. Cache to�n b? page: Nguy�n c? page ?ng v?i url nh?t d?nh du?c luu v�o cache, c�c truy c?p ti?p theo d?n c�ng url n�y s? include ngay l?p t?c file cache du?c t?o ra tru?c d�, do d� s? gi?m t?i da v? th?i gian do server kh�ng ph?i x? l� g� c? (k? c? truy c?p DB). C�ch n�y don gi?n nhung kh�ng linh ho?t, v� kh� th?c thi v� d? li?u tr�n c�c trang l� dynamic.

2. Cache t?ng ph?n c?a page: Ta s? ch? cache m?t ph?n c?a page m� t?i d� d? li?u �t b? thay d?i. �i?u n�y d?m b?o client s? nh?n du?c d? li?u tr? v? l� ch�nh x�c m� kh�ng ph?i d? li?u cu t? file cache.

3. Cache SQL: Khi c�ng c�u l?nh SQL du?c g?i di g?i l?i, th� ch? l?nh d?u ti�n du?c g?i d?n DB server, trong c�c Framework d?u c� cache SQL.

�? b?t d?u th� ta ph?i t?o m?t thu m?c cache d? ch?a c�c file cache cho ?ng d?ng c?a ta, kh�ng l�n d? c�c file cache lung tung v� d? c� th? cache file ta c?n chmod cho thu m?c ph?i c� quy?n ghi v� d?c, do d� d? d? chmod v� b?o m?t ta c?n t?o ri�ng thu m?c cache, vi?c d?t cache key cung c?n ch� � d? d? ph�n bi?t. Sau d�y l� m?t v� d? don gi?n cache d? li?u:

CODE

$cacheFile = 'cache/name_file_cache.php';
if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) ){
$content = file_get_contents($cacheFile);
echo $content;
} else{
ob_start();
echo '

Hello world to cache

';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content);
echo $content;
}
?>


SQL cache:

CODE

$file = 'sql_cache.txt';
$expire = 86400; // 24 hours
if (file_exists($file) &&
filemtime($file) > (time() - $expire)) {
$records = unserialize(file_get_contents($file));
} else {
$link = mysql_connect('localhost','username','password')
or die (mysql_error());
mysql_select_db('shop')
or die (mysql_error());
/* form SQL query */
$query = "SELECT * FROM categories";
$result = mysql_query($query)
or die (mysql_error());
while ($record = mysql_fetch_array($result) ) {
$records[] = $record;
}
$OUTPUT = serialize($records);
$fp = fopen($file,"w");
fputs($fp, $OUTPUT);
fclose($fp);
} // end else

// Query results are in $records array
foreach ($records as $id=>$row) {
if ($row['category_id'] == $_REQUEST['category_id']) {
// category selected - print bold
print ''.$row['category_name'].'
';
} else {
// other category - print regular
print $row['category_name'].'
';
}
} // end foreach


D? li?u out put s? ch?a ki?u v� size, c� d?ng:

CODE

a:1:{i:0;a:6:{i:0;s:1:"1";s:11:"category_id";s:1:"1";i:1;s:9:"Computers";s:13:"category_name";s:9:
"Computers" ;i:2;s:25:"Description for computers";s:20:"category_description"
;s:25:"Description for computers";}}


Ch�c c�c b?n th�nh c�ng !