2008年12月16日

ADODB 技巧收集及分享 - CacheExecute

CacheExecute([$secs2cache,]$sql,$inputarr=false)

類似於 Execute 函數,除了將資料集暫存在 $ADODB_CACHE_DIR 指定的目錄裡 $secs2cache 秒以及$inputarr只接受一維陣列外。如果 CacheExecute() 呼叫時有相同的參數$sql、$inputarr、資料庫、使用者userid,而且快取也沒有過期,那麼快取中的資料集將會被傳回。

include('adodb.inc.php');
include(
'tohtml.inc.php');
$ADODB_CACHE_DIR = '/usr/local/ADOdbcache';
$conn = &ADONewConnection('mysql');
$conn->PConnect('localhost','userid','password','database');
$rs = $conn->CacheExecute(15, 'select * from table'); # cache 15 secs
rs2html($rs); /* recordset to html table */

另外自 ADOdb 1.80以後 $secs2cache 參數是選擇性的:

$conn->Connect(...);
$conn->cacheSecs = 3600*24; // cache 24 hours
$rs = $conn->CacheExecute('select * from table');

假如省略了 $secs2cache ,我們可以在$connection->cacheSecs (預設是3600秒或1小時)裡使用這個值,CacheExecute()只有用在SELECT指令中。

效能備註:曾經作了一些效能測試,並且發現這些快取的效益 極為顯著。尤其是在資料庫伺服器運作效率慢於WEB伺服器或資料庫的負荷非常重的時候。ADODB的快取好在它減少了資料庫伺服器的負荷。當然,如果你的 資料庫伺服器負荷不大,而且運作速度也比WEB伺服器快,那快取反而會降低效能。