| ADOdb Library for PHP Manual | ||
|---|---|---|
| Prev | Caching of Recordsets |
Next |
ADOdb now supports caching of recordsets in the file system using the CacheExecute( ), CachePageExecute( ) and CacheSelectLimit( ) functions. There are similar to the non-cache functions, except that they take a new first parameter, $secs2cache.
An example:
include('adodb.inc.php'); # load code common to ADOdb
$ADODB_CACHE_DIR = '/usr/ADODB_cache';
$conn = &ADONewConnection('mysql'); # create a connection
$conn->PConnect('localhost','userid','','agora');# connect to MySQL, agora db
$sql = 'select CustomerName, CustomerID from customers';
$rs = $conn->CacheExecute(15,$sql);
The first parameter is the number of seconds to cache the query. Subsequent calls to that query will used the cached version stored in $ADODB_CACHE_DIR. To force a query to execute and flush the cache, call CacheExecute() with the first parameter set to zero. Alternatively, use the CacheFlush($sql) call.
For the sake of security, we recommend you set register_globals=off in php.ini if you are using $ADODB_CACHE_DIR.
In ADOdb 1.80 onwards, the secs2cache parameter is optional in CacheSelectLimit() and CacheExecute(). If you leave it out, it will use the $connection->cacheSecs parameter, which defaults to 60 minutes. The following are equivalent:
# (1)
$rs = $db->SelectLimit(30, 'select * from table', 10);
# (2)
$db->cacheSsecs = 30;
$rs = $db->SelectLimit('select * from table', 10);
$conn->Connect(...);
$conn->cacheSecs = 3600*24; # cache 24 hours
$rs = $conn->CacheExecute('select * from table');
Please note that magic_quotes_runtime should be turned off. Do not change $ADODB_FETCH_MODE (or SetFetchMode)
as the cached recordset will use the $ADODB_FETCH_MODE set when the query was executed.
You can also share cached recordsets on a memcache server. The memcache API supports one or more pooled hosts. Only if none of the pooled servers can be contacted will a connect error be generated.
Example below:
More info on memcache can be found at http://www.danga.com/memcached/.
There is also a caching API since 4.99/5.05. Two implementations of the API are already available providing file and memcache support.
The new API for creating your custom caching class uses 2 globals:
MemCache support
$db = NewADOConnection($driver);
$db->memCache = true;
$db->memCacheHost = array($ip1, $ip2, $ip3); /// $db->memCacheHost = $ip1; will work too
$db->memCachePort = 11211; /// this is default memCache port
$db->memCacheCompress = false; /// Use 'true' to store the item compressed (uses zlib)
$db->Connect(...);
$db->CacheExecute($sql);
Caching API
include "/path/to/adodb.inc.php";
$ADODB_CACHE_CLASS = 'MyCacheClass';
class MyCacheClass extends ADODB_Cache_File
{
var $createdir = false; // do not set this to true unless you use temp directories in cache path
function writecache($filename, $contents,$debug=false){...}
function &readcache($filename, &$err, $secs2cache, $rsClass){ ...}
:
}
$DB = NewADOConnection($driver);
$DB->Connect(...); ## MyCacheClass created here and stored in $ADODB_CACHE global variable.
$data = $rs->CacheGetOne($sql); ## MyCacheClass is used here for caching...
Prev
Home
Next
Data Source Names
Up
Pivot Tables