PHP 世界中緩沖是一個熱門的話題,因為 PHP 產生的動態頁面,每次用戶請求都需要重新計算,不論請求的結果是否一樣,同時,PHP 每次都會編譯一次腳本。這樣的超負荷運轉對一個流量很高的網站來說肯定難以忍受。幸運的是, Web 的結果可以緩沖,而不需要重新運行和編譯腳本,商品化的產品像 ZendCache 或者開源的 Alternate PHP Cache都提供了把 PHP 腳本編譯為字節代碼并緩沖的辦法。
PEAR 的緩沖包提供了緩沖動態內容,數據庫查詢和 PHP 函數調用的框架。
就像 Perl 有 CPAN, TeX 有 CTAN,PHP 也有自己的中心資源庫,存放類,庫和模塊。這個庫稱為 PEAR(PHP Extension and Add-On Repository)。
本文假設你已經安裝了 PEAR 環境,如果沒有的話,可以去 PHP 網站下載。
PEAR 的緩沖包包含一個總體的緩沖類和幾個特別的子類。緩沖類使用容器類來存貯和管理緩沖數據。
下面是 PEAR 緩沖當前所包含的容器,以及各自的參數:
file -- file 容器在文件系統存儲了緩沖的數據,是最快的容器。
cache_dir -- 這是容器存儲文件的目錄。
filename_prefix -- 緩沖文件的前綴,例如:"cache_"。
shm -- shm 容器把緩沖數據放入共享內存,基準測試顯示,目前的實現下,這個容器的速度要比文件容器慢。
shm_key -- 共享內存使用的鍵值。
shm_perm -- 使用共享內存數據段的權限。
shm_size -- 分配共享內存的大小。
sem_key -- 信號燈的鍵值。
sem_perm -- 信號燈的權限。
db -- PEAR 的數據庫抽象層。
dsn -- 數據庫連接的 DSN ??梢詤⒖?PEAR 的 DB 文檔。
cache_table -- 表的名字。
phplib -- phplib 容器使用數據庫抽象層存儲緩沖。
db_class
db_file
db_path
local_file
local_path
ext/dbx -- PHP 的數據庫抽象層擴展,如果像把緩沖存入數據庫,可以采用這個容器。
module
host
db
username
password
cache_table
persistent
使用 PEAR Cache 所得到的性能提升取決于你所選擇的緩沖容器,例如,把數據庫的結果再次存入數據庫緩沖中就顯得毫無意義。
PEAR Cache 的函數緩沖模塊能把任何函數或者方法的結果緩沖,不論是 PHP 的內置函數還是用戶自定義函數,他缺省采用文件容器,把緩沖數據放入到一個叫做
function_cache 的目錄。
Cache_Function 類的構造器可以有三個可選的參數:
$container :緩沖容器的名字。
$container_options :緩沖容器的數組參數。
$expires:緩沖對象過期的時間(秒數)。
普通的函數調用采用 Cache_Function 類的 call() 方法時,就能觸發緩沖。調用 call() 很容易,的一個參數是函數的名字,然后是函數的參數,第二個參數是要調用函數中的第一個,依此類推,我們來看例子:
例 1: 緩沖函數和方法的調用
// 調用 PEAR Cache 的函數緩沖。
<?php
require_once ’Cache/Function.php’;
// 定義一些類和函數。
class foo {
function bar($test) {
echo "foo::bar($test)<br>";
}
}
class bar {
function foobar($object) {
echo ’$’.$object.’->foobar(’.$object.’)
’;
}
}
$bar = new bar;
function foobar() {
echo ’foobar()’;
}
// 取得 Cache_Function 對象
$cache = new Cache_Function();
// 對 foo 類的靜態函數 bar() 作緩沖(foo::bar())。
$cache->call(’foo::bar’, ’test’);
// $bar->foobar()
$cache->call(’bar->foobar’, ’bar’);
$cache->call(’foobar’);
?>
下面我們采用 Cache_Output 來把輸出作緩沖:
例子 2: 緩沖腳本的輸出
// 加載 PEAR Cache 的輸出緩沖
<?php
require_once ’Cache/Output.php’;
$cache = new Cache_Output(’file’, array(’cache_dir’ => ’.’) );
// 計算要緩沖頁面的標記,我們假定頁面的緩沖取決于
// URL, HTTP GET 和 POST 變量以及 cookies。
$cache_id = $cache->generateID(array(’url’ => $REQUEST_URI, ’post’ => $HTTP_POST_VARS, ’cookies’ => $HTTP_COOKIE_VARS) );
// 查詢緩沖
if ($content = $cache->start($cache_id)) {
// 緩沖命中
echo $content;
die();
}
// 緩沖丟失
// -- 在這里插入內容產生代碼 --
// 把頁面存入緩沖
echo $cache->end();
?>
利用 Cache_Output 類,很容易把一個動態的數據庫驅動的網站應用轉化為靜態,從而極大的提升站點的性能。
越來越多的站點在采用 GZIP 壓縮 HTML 內容,這樣減少了服務器的帶寬消耗,對于使用 Modem 上網的用戶來說也能受益不少。
Cache_OutputCompression 擴展了 Cache_Output 類的功能,他把 GZIP 壓縮的 HTML 內容進行緩沖,從而節省了 CPU 壓縮的時間。
最后,我們來定制一個應用,綜合的來解釋 PEAR 緩沖機制的整體框架。我們定義一個叫做 MySQL_Query_Cache 的類,緩沖 SELECT 的查詢結果。
我們首先定義類的變量:
<?php
require_once ’Cache.php’;
class MySQL_Query_Cache extends Cache {
var $connection = null;
var $expires = 3600;
var $cursor = 0;
var $result = array();
function MySQL_Query_Cache($container = ’file’,
$container_options = array(’cache_dir’=> ’.’,
’filename_prefix’ => ’cache_’), $expires = 3600)
{
$this->Cache($container, $container_options);
$this->expires = $expires;
}
function _MySQL_Query_Cache() {
if (is_resource($this->connection)) {
mysql_close($this->connection);
}
$this->_Cache();
}
}
?>
在正式開始之前,我們需要一些輔助函數。
function connect($hostname, $username, $password, $database) {
$this->connection = mysql_connect($hostname, $username, $password) or trigger_error(’數據庫連接失??!’, E_USER_ERROR);
mysql_select_db($database, $this->connection) or trigger_error(’數據庫選擇失??!’, E_USER_ERROR);
}
function fetch_row() {
if ($this->cursor < sizeof($this->result)) {
return $this->result[$this->cursor++];
} else {
return false;
}
}
function num_rows() {
return sizeof($this->result);
}
?>
下面我們來看怎樣緩沖:
<?php
function query($query) {
if (stristr($query, ’SELECT’)) {
// 計算查詢的緩沖標記
$cache_id = md5($query);
// 查詢緩沖
$this->result = $this->get($cache_id, ’mysql_query_cache’);
if ($this->result == NULL) {
// 緩沖丟失
$this->cursor = 0;
$this->result = array();
if (is_resource($this->connection)) {
// 盡可能采用 mysql_unbuffered_query()
if (function_exists(’mysql_unbuffered_query’)) {$result = mysql_unbuffered_query($query, $this->connection);
} else {$result = mysql_query($query, $this->connection);
}
// 取出所有查詢結果
while ($row = mysql_fetch_assoc($result)) {$this->result[] = $row;
}
// 釋放 MySQL 結果資源
mysql_free_result($result);
// 把結果緩沖
$this->save($cache_id, $this->result, $this->expires, ’mysql_query_cache’);
}
}
} else {
// 沒有查詢結果,不需要緩沖
return mysql_query($query, $this->connection);
}
}
?>
例 3: 使用 MySQL 查詢緩沖
<?php require_once ’MySQL_Query_Cache.php’;
$cache = new MySQL_Query_Cache();
$cache->connect(’hostname’, ’username’, ’password’, ’database’);
$cache->query(’select * from table’);
while ($row = $cache->fetch_row()) {
echo ’<p>’;
print_r($row);
echo ’</p>’;
}
?>
原文轉自:http://www.anti-gravitydesign.com