天天看點

php提高網站性能之PHP資料緩存技術詳解

資料緩存是web開發中常用的一種性能優化方法。目前主要檔案緩存或者資料庫緩存兩種形式,資料庫緩存資料庫不是什麼不可能的事情,的确也是很好很重要的。我認為傳統資料庫主要是從業務層、子產品設計等方面來考慮的,而緩存資料庫主要是從實作層來設計的,主要是為了緩存常用的多表查詢之類的。這裡主要将的是檔案緩存,網上很多資料了,這裡我轉載了一些原理資料。

    Cache是“以空間換時間”政策的典型應用模式,是提高系統性能的一種重要方法。緩存的使用在大通路量的情況下能夠極大的減少對資料庫操作的次數,明顯降低系統負荷提高系統性能。相比頁面的緩存,結果集是一種“原始資料”不包含格式資訊,資料量相對較小,而且可以再進行格式化,是以顯得相當靈活。由于PHP是“一邊編譯一邊執行”的腳本語言,某種程度上也提供了一種相當友善的結果集緩存使用方法——通過動态include相應的資料定義代碼段的方式使用緩存。如果在“RamDisk”上建緩存的話,效率應該還可以得到進一步的提升。以下是一小段示例代碼,供參考。

 1

php提高網站性能之PHP資料緩存技術詳解

function load_data($id,$cache_lifetime) {     

 2

php提高網站性能之PHP資料緩存技術詳解

 3

php提高網站性能之PHP資料緩存技術詳解

// the return data     

 4

php提高網站性能之PHP資料緩存技術詳解

 5

php提高網站性能之PHP資料緩存技術詳解

$data = array();     

 6

php提高網站性能之PHP資料緩存技術詳解

 7

php提高網站性能之PHP資料緩存技術詳解

// make cache filename     

 8

php提高網站性能之PHP資料緩存技術詳解

 9

php提高網站性能之PHP資料緩存技術詳解

$cache_filename = ‘cache_‘.$id.‘.php‘;     

10

php提高網站性能之PHP資料緩存技術詳解

11

php提高網站性能之PHP資料緩存技術詳解

// check cache file‘s last modify time     

12

php提高網站性能之PHP資料緩存技術詳解

13

php提高網站性能之PHP資料緩存技術詳解

$cache_filetime = filemtime($cache_filename);     

14

php提高網站性能之PHP資料緩存技術詳解

15

php提高網站性能之PHP資料緩存技術詳解

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

16

php提高網站性能之PHP資料緩存技術詳解

17

php提高網站性能之PHP資料緩存技術詳解

//** the cache is not expire     

18

php提高網站性能之PHP資料緩存技術詳解

19

php提高網站性能之PHP資料緩存技術詳解

include($cache_filename);     

20

php提高網站性能之PHP資料緩存技術詳解

21

php提高網站性能之PHP資料緩存技術詳解

} else {     

22

php提高網站性能之PHP資料緩存技術詳解

23

php提高網站性能之PHP資料緩存技術詳解

//** the cache is expired     

24

php提高網站性能之PHP資料緩存技術詳解

25

php提高網站性能之PHP資料緩存技術詳解

// load data from database     

26

php提高網站性能之PHP資料緩存技術詳解

27

php提高網站性能之PHP資料緩存技術詳解

// 

php提高網站性能之PHP資料緩存技術詳解

28

php提高網站性能之PHP資料緩存技術詳解

29

php提高網站性能之PHP資料緩存技術詳解

while ($dbo->nextRecord()) {     

30

php提高網站性能之PHP資料緩存技術詳解

31

php提高網站性能之PHP資料緩存技術詳解

// $data[] = 

php提高網站性能之PHP資料緩存技術詳解

32

php提高網站性能之PHP資料緩存技術詳解

33

php提高網站性能之PHP資料緩存技術詳解

}     

34

php提高網站性能之PHP資料緩存技術詳解

35

php提高網站性能之PHP資料緩存技術詳解

// format the data as a php file     

36

php提高網站性能之PHP資料緩存技術詳解

37

php提高網站性能之PHP資料緩存技術詳解

$data_cache = "    

38

php提高網站性能之PHP資料緩存技術詳解

39

php提高網站性能之PHP資料緩存技術詳解

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

40

php提高網站性能之PHP資料緩存技術詳解

41

php提高網站性能之PHP資料緩存技術詳解

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

42

php提高網站性能之PHP資料緩存技術詳解

43

php提高網站性能之PHP資料緩存技術詳解

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

44

php提高網站性能之PHP資料緩存技術詳解

45

php提高網站性能之PHP資料緩存技術詳解

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

46

php提高網站性能之PHP資料緩存技術詳解

47

php提高網站性能之PHP資料緩存技術詳解

$data_cache .= ";);";     

48

php提高網站性能之PHP資料緩存技術詳解

49

php提高網站性能之PHP資料緩存技術詳解

50

php提高網站性能之PHP資料緩存技術詳解

51

php提高網站性能之PHP資料緩存技術詳解

$data_cache = "?>";     

52

php提高網站性能之PHP資料緩存技術詳解

53

php提高網站性能之PHP資料緩存技術詳解

// save the data to the cache file     

54

php提高網站性能之PHP資料緩存技術詳解

55

php提高網站性能之PHP資料緩存技術詳解

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

56

php提高網站性能之PHP資料緩存技術詳解

57

php提高網站性能之PHP資料緩存技術詳解

fputs($fd,$data_cache);     

58

php提高網站性能之PHP資料緩存技術詳解

59

php提高網站性能之PHP資料緩存技術詳解

fclose($fd);     

60

php提高網站性能之PHP資料緩存技術詳解

61

php提高網站性能之PHP資料緩存技術詳解

62

php提高網站性能之PHP資料緩存技術詳解

63

php提高網站性能之PHP資料緩存技術詳解

64

php提高網站性能之PHP資料緩存技術詳解

65

php提高網站性能之PHP資料緩存技術詳解

return $data;     

66

php提高網站性能之PHP資料緩存技術詳解

67

php提高網站性能之PHP資料緩存技術詳解

68

php提高網站性能之PHP資料緩存技術詳解

69

php提高網站性能之PHP資料緩存技術詳解

?>    

 适用情況:

    1.資料相對比較穩定,主要是讀取操作。

    2.檔案操作要比資料庫操作快。

    3.複雜資料通路,大資料量通路,密集資料通路,系統資料庫負載極重。

    4.Web/DB分離結構或者多Web單DB結構。

////////////補充

php提高網站性能之PHP資料緩存技術詳解

<?php     

php提高網站性能之PHP資料緩存技術詳解

function cache_isvalid($cacheid,$expire=300) {     

php提高網站性能之PHP資料緩存技術詳解

@clearstatcache();     

php提高網站性能之PHP資料緩存技術詳解

if (!@file_exists($cacheid)) return false;     

php提高網站性能之PHP資料緩存技術詳解

if (!($mtime=@filemtime($cacheid))) return false;     

php提高網站性能之PHP資料緩存技術詳解

$nowtime=mktime();     

php提高網站性能之PHP資料緩存技術詳解

if (($mtime+$expire)<$nowtime) {     

php提高網站性能之PHP資料緩存技術詳解

return false;     

php提高網站性能之PHP資料緩存技術詳解

}else{     

php提高網站性能之PHP資料緩存技術詳解

return true;     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

function cache_write($cacheid,$cachecontent) {     

php提高網站性能之PHP資料緩存技術詳解

$retry=100;     

php提高網站性能之PHP資料緩存技術詳解

for ($i=0;$i<$retry;$i++) {     

php提高網站性能之PHP資料緩存技術詳解

$ft=@fopen($cacheid,"wb");     

php提高網站性能之PHP資料緩存技術詳解

if ($ft!=false) break;     

php提高網站性能之PHP資料緩存技術詳解

if ($i==($retry-1)) return false;     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

@flock($ft,LOCK_UN);     

php提高網站性能之PHP資料緩存技術詳解

@flock($ft,LOCK_EX|LOCK_NB);     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

$tmp=@fwrite($ft,$cachecontent);     

php提高網站性能之PHP資料緩存技術詳解

if ($tmp!=false) break;     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

@fclose($ft);     

php提高網站性能之PHP資料緩存技術詳解

@chmod($cacheid,0777);     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

function cache_fetch($cacheid) {     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

$ft=@fopen($cacheid,"rb");     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

$cachecontent='';     

php提高網站性能之PHP資料緩存技術詳解

while (!@feof($ft)) {     

php提高網站性能之PHP資料緩存技術詳解

$cachecontent.=@fread($ft,4096);     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

return $cachecontent;     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

function cache_clear_expired($cachedirname,$expire=300) {     

php提高網站性能之PHP資料緩存技術詳解

$cachedir=@opendir($cachedirname);     

php提高網站性能之PHP資料緩存技術詳解

while (false!==($userfile=@readdir($cachedir))) {     

php提高網站性能之PHP資料緩存技術詳解

if ($userfile!="." and $userfile!=".." and substr($userfile,-4,4)=='.htm') {     

php提高網站性能之PHP資料緩存技術詳解

$cacheid=$cachedirname.'/'.$userfile;     

php提高網站性能之PHP資料緩存技術詳解

if (!cache_isvalid($cacheid,$expire)) @unlink($cacheid);     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

@closedir($cachedir);     

php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解
php提高網站性能之PHP資料緩存技術詳解

?>