資料緩存是web開發中常用的一種效能最佳化方法。目前主要檔案快取或資料庫快取兩種形式,資料庫快取資料庫不是什麼不可能的事情,的確也是很好很重要的。我認為傳統資料庫主要是從業務層、模組設計等方面來考慮的,而快取資料庫主要是從實作層來設計的,主要是為了快取常用的多表查詢之類的。這裡主要將的是檔案緩存,網路上很多資料了,這裡我轉載了一些原理資料。
Cache是「以空間換時間」策略的典型應用模式,是提升系統效能的重要方法。快取的使用在大訪問量的情況下能夠極大的減少對資料庫操作的次數,明顯降低系統負載提高系統效能。相比頁面的緩存,結果集是一種「原始資料」不包含格式信息,資料量相對較小,而且可以再進行格式化,所以顯得相當靈活。由於PHP是「一邊編譯一邊執行」的腳本語言,某種程度上也提供了相當方便的結果集快取使用方法——透過動態include對應的資料定義程式碼段的方式使用快取。如果在「RamDisk」上建造快取的話,效率應該還可以得到進一步的提升。以下是一小段範例程式碼,供參考。
<span><code><span><br><span><? <BR></SPAN><SPAN>// load data with cache <br></SPAN><SPAN>function </SPAN><SPAN>load_data</SPAN><SPAN>(</SPAN><SPAN>$id</SPAN><SPAN>,</SPAN><SPAN>$cache_lifetime</SPAN><SPAN>) { <br></SPAN><SPAN>// the return data <br></SPAN><SPAN>$data </SPAN><SPAN>= array(); <br></SPAN><SPAN>// make cache filename <br></SPAN><SPAN>$cache_filename </SPAN><SPAN>= </SPAN><SPAN>‘cache_‘</SPAN><SPAN>.</SPAN><SPAN>$id</SPAN><SPAN>.</SPAN><SPAN>‘</SPAN><SPAN>.</SPAN><SPAN>php‘</SPAN><SPAN>; <br></SPAN><SPAN>// check cache file‘s last modify time <br></SPAN><SPAN>$cache_filetime </SPAN><SPAN>= </SPAN><SPAN>filemtime</SPAN><SPAN>(</SPAN><SPAN>$cache_filename</SPAN><SPAN>); <br>if (</SPAN><SPAN>time</SPAN><SPAN>() - </SPAN><SPAN>$cache_filetime </SPAN><SPAN><= </SPAN><SPAN>$cache_lifetime</SPAN><SPAN>) { <br></SPAN><SPAN>//** the cache is not expire <br></SPAN><SPAN>include(</SPAN><SPAN>$cache_filename</SPAN><SPAN>); <br>} else { <br></SPAN><SPAN>//** the cache is expired <br>// load data from database <br>// ... <br></SPAN><SPAN>while (</SPAN><SPAN>$dbo</SPAN><SPAN>-></span><span>nextRecord</span><span>()) { <br></span><span>// $data[] = ... <br></span><span>} <br></span><span>// format the data as a php file <br></span><span>$data_cache </span><span>= </span><span>" <br>while (list($key, $val) = each($data)) { <br>$data_cache .= "</span><span>$data</span><span>[</span><span>‘$key‘</span><span>]=array(</span><span>‘</span><span>"; <br>$data_cache .= "</span><span>‘NAME‘</span><span>=></span><span>""</span><span>.</span><span>qoute</span><span>(</span><span>$val</span><span>[</span><span>‘NAME‘</span><span>]).</span><span>""," <br></span><span>$data_cache </span><span>.= </span><span>"‘VALUE‘=>""</span><span>.</span><span>qoute</span><span>(</span><span>$val</span><span>[</span><span>‘VALUE‘</span><span>]).</span><span>""" <br></span><span>$data_cache </span><span>.= </span><span>";);rn"</span><span>; <br>} <br></span><span>$data_cache </span><span>= </span><span>"?>rn"</span><span>; <br></span><span>// save the data to the cache file <br></span><span>if (</span><span>$fd </span><span>= </span><span>fopen</span><span>(</span><span>$cache_filename</span><span>,</span><span>‘w</span><span>+</span><span>‘</span><span>)) { <br></span><span>fputs</span><span>(</span><span>$fd</span><span>,</span><span>$data_cache</span><span>); <br></span><span>fclose</span><span>(</span><span>$fd</span><span>); <br>} <br>} <br>return </span><span>$data</span><span>; <br>} <br></span><span>?></span> <br></span>
// load data with cache 以上就介紹了2009nba總決賽資料 PHP資料快取技術,包含了2009nba總決賽資料的內容,希望對PHP教學有興趣的朋友有所幫助。