ADOdb: Caching of Recordset



It's cute feature in adodb, caching of recordset. It will fast result your query.

Several method cache recordset:

CacheExecute()
CachePageExecute()
CacheSelectLimit()

CacheExecute()

Similar to Execute, except that the recordset is cached for $secs2cache seconds in the $ADODB_CACHE_DIR directory. If CacheExecute() is called again with the same parameters, same database, same userid, same password, and the cached recordset has not expired, the cached recordset is returned

    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 

CachePageExecute()

formula:

CachePageExecute($secs2cache, $sql, $nrows, $page, $inputarr=false) 

Used for pagination of recordset. $page is 1-based.

    include_once('adodb.inc.php'); 
    include_once('adodb-pager.inc.php'); 
    session_start(); 

    $db = NewADOConnection('mysql'); 
    $db->Connect('localhost','root','','xphplens'); 
    $sql = "select * from adoxyz "; 

    $pager = new ADODB_Pager($db,$sql); 
    $pager->Render($rows_per_page=5); 

CacheSelectLimit()

Formula:

CacheSelectLimit([$secs2cache,] $sql, $numrows=-1,$offset=-1,$inputarr=false)

Similar to SelectLimit, except that the recordset returned is cached for $secs2cache seconds in the $ADODB_CACHE_DIR directory.

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

I have done some benchmarks and found that they vary so greatly that it's better to talk about when caching is of benefit. When your database server is much slower than your Web server or the database is very overloaded then ADOdb's caching is good because it reduces the load on your database server. If your database server is lightly loaded or much faster than your Web server, then caching could actually reduce performance (note from manual).



Series this article:
ADOdb: Introduction
ADOdb: Connection Statement
ADOdb: Advance Select Statement
ADOdb: How to show tables
ADOdb: How to show fields
ADOdb: How to show databases
ADOdb: Caching of Recordset
ADOdb: Recordset to HTML
ADOdb: Multi Database Connection
ADOdb: Data Dictionary
ADOdb: Quick Export Data
ADOdb: Insert data style
ADOdb: Replace Data
ADOdb: Log Query
PHP ADOdb: Understanding Pivot Table For Reporting
PHP ADOdb: Creating Pivot Table
PHP ADOdb: Creating Query to Build Pivot Table


Tag: adodb, database, database layer Category: PHP Database Post : October 30th 2007 Read: 5,755 Bookmark and Share

blog comments powered by Disqus