Provides a database level storage system for the cache controller. This class requires a table with the following schema:
CREATE TABLE `dataCache` ( `cacheId` varchar(255) NOT NULL, `data` mediumtext NOT NULL, `createDate` datetime NOT NULL, `updateDate` datetime NOT NULL, `lifetime` int(10) NOT NULL default '3600', UNIQUE KEY `cacheId` (`cacheId`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
dataCache can be renamed however you wish to, but it must exist. This class is abstract and requires a specific database backend implementation.
Sub-classes need to implement the following methods:
Note: there are some small differences between this writer and the file writer. Using isCached() and isExpired() will automatically trigger a load from the database. If not, each would require a separate query; a select 1 from..., select (now()-unix_timestamp(update_time))>lifetime from...) and a select.
Some examples of using the database writer storage backend:
// store an item using defaults
$oWriter = new cacheWriterDatabase('dbName', 'tbCacheName');
$oWriter->setCacheId('MyCachedObject');
$oWriter->setData(new stdClass());
$oWriter->save();
// check if cached and fetch the previous item
$oWriter = new cacheWriterDatabase('dbName', 'tbCacheName');
$oWriter->setCacheId('MyCachedObject');
if ( $oWriter->isCached() ) {
$oWriter->getData();
//...
}
// clear the cache
$oWriter = new cacheWriterDatabase('dbName', 'tbCacheName');
$oWriter->setCacheId('MyCachedObject');
$oWriter->delete();
if ( $oWriter->isCached() ) {
// this should not execute...
}
public __construct([$inCacheDatabase = null], [$inCacheTable = null], [$inCacheId = null])
Returns a new database writer for the cache controller
public getCacheDatabase()
Returns $_CacheDatabase
public setCacheDatabase($inCacheDatabase)
Set $_CacheDatabase to $inCacheDatabase
public getCacheTable()
Returns $_CacheTable
public setCacheTable($inCacheTable)
Set $_CacheTable to $inCacheTable
public getLoaded()
Returns $_Loaded
public setLoaded($inLoaded)
Set $_Loaded to $inLoaded
Posted by: Scorpio Documentor (Writer), in Cache on 19 Nov 2009 @ 20:30
Tags: cache, cachewriter, cachewriterdatabase,
This
work is licenced under a
Creative Commons Licence.