ob_start ob_get_flush 這些函數是緩衝技術的一種,是減輕伺服器壓力的,直到項目開發用到才知道混淆了和緩衝的概念,
這些像ob_start ob_get_flush這些函數都是為了在編程中字串輸出到用戶端上去為了延長時間而用到的技術,延遲輸出(字串先發送到緩衝區需要時在輸出到瀏覽器),是一種輸出技巧。最常見的應用是靜態化技術(可以實現靜態緩衝):
把要輸出代碼的先儲存到緩衝區在用ob_get_contents();取得內容寫入檔案
php ob_start 與 ob_end_flush() 是 php 的緩衝輸出函數。
ob_start([string output_callback])- 開啟輸出緩衝區,所有的輸出資訊不在直接發送到瀏覽器,而是儲存在輸出緩衝區裡面,可選得回呼函數用於處理輸出結果資訊。
ob_end_flush - 結束(發送)輸出緩衝區的內容,關閉輸出緩衝區。
php 輸出東西,會儲存在一個 php 維護的記憶體裡,稱為 buffer 也行,緩衝也行,都是一個意思。然後當這個 buffer 滿了,php 會自動往 web server 發送這些資料。
也就是說每次 echo,並不一定會輸出東西,而是儲存在 buffer 裡。
ob_start() 的意思,可以理解為(但是實際上和我下面的說法有區別),這個 buffer 由 ob_ 系列函數來來控制,也就是,PHP 不會維護自己的 buffer,不會自動把buffer 的內容自動發送到 web server,直到你 ob_end() 或者類似的 ob 操作。
ob_函數一般用來捕獲當前的輸出,跟效率是沒什麼關係的。至於為什麼捕獲輸出,原因很多,例如我捕捉輸出,緩衝到一個檔案裡,下次請求就可以直接讀這個 cache 檔案的內容作為輸出了。
代碼如下 |
複製代碼 |
ob_start(); 2 內容 3 echo ob_get_contents() ; |
例子
代碼如下 |
複製代碼 |
<?php //開啟緩衝區 ob_start(); ?> php頁面的全部輸出 <?php //取得php頁面輸出的全部內容 $content = ob_get_contents(); //建立一個檔案,並開啟,準備寫入 $fp = fopen(“1.html”, “w”); //把php頁面的內容全部寫1.html fwrite($fp, $content); fclose($fp); ?> |
而緩衝是一個很大的概念,一般用來解決大型網站高負載問題,像下面雨中漫步講的 就是一種緩衝技術的實現,當然除了記憶體緩衝還有檔案、頁面緩衝,比如想uchome 中系統組態變數,一般不需要改變的設定組建檔案是檔案快取,用到這些資料直接通過讀取檔案讀取而不直接從資料庫讀取,這些都是為了避免重新查詢資料減輕訪問壓力問題而設定的。
網上關於 PHP 緩衝類的資料很多,不過這個類應該是我見過功能滿足需求,但又無比簡潔的一個。廢話不多說,直接看代碼吧!
使用說明:
1、執行個體化
代碼如下 |
複製代碼 |
$cache = new Cache(); |
2、設定緩衝時間和緩衝目錄
$cache = new Cache(60, '/any_other_path/');第一個參數是緩衝秒數,第二個參數是緩衝路徑,根據需要配置。
預設情況下,緩衝時間是 3600 秒,緩衝目錄是 cache/
3、讀取緩衝
代碼如下 |
複製代碼 |
$value = $cache->get('data_key');4、寫入緩衝 $value = $cache->put('data_key', 'data_value');完整執行個體: $cache = new Cache(); //從緩衝從讀取索引值 $key 的資料 $values = $cache->get($key); //如果沒有快取資料 if ($values == false) { //insert code here... //寫入索引值 $key 的資料 $cache->put($key, $values); } else { //insert code here... } Cache.class.php <?php class Cache { private $cache_path;//path for the cache private $cache_expire;//seconds that the cache expires //cache constructor, optional expiring time and cache path public function Cache($exp_time=3600,$path="cache/"){ $this->cache_expire=$exp_time; $this->cache_path=$path; } //returns the filename for the cache private function fileName($key){ return $this->cache_path.md5($key); } //creates new cache files with the given data, $key== name of the cache, data the info/values to store public function put($key, $data){ $values = serialize($data); $filename = $this->fileName($key); $file = fopen($filename, 'w'); if ($file){//able to create the file fwrite($file, $values); fclose($file); } else return false; } //returns cache for the given key public function get($key){ $filename = $this->fileName($key); if (!file_exists($filename) || !is_readable($filename)){//can't read the cache return false; } if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired $file = fopen($filename, "r");// read data file if ($file){//able to open the file $data = fread($file, filesize($filename)); fclose($file); return unserialize($data);//return the values } else return false; } else return false;//was expired you need to create new } } ?> |