hi,
i’m considering running memcache daemon on a site, so i’m just trying to get my head around caching objects. i’ve got a few questions:
- do i have to manually add things to the cache with xpdo->toCache() and xpdo->fromCache(), or do i only need to specify cache time-to-live values as part of xpdo->getObject()?
- do i need to initialise the cache manager first? From the docs i can see that xpdo->$_cacheEnabled is false by default - how do i enable caching? do i just set $xpdo->$_cacheEnabled = true?
- i can see the xpdo->getCacheManager method, but how could i override it with my own that inherits from xpdoCacheManager? to enable use of memcache do i just have to set $xpdo->config[’cache_db_handler’] to Memcache?
are there any examples of this in the 0.9.7 code or anywhere else i can have a look at?
cheers
XPDO_OPT_CACHE_DB => true
class LTC extends xPDO {
function __construct() {
$this->_loadConfig();
$this->config = array_merge(array(
XPDO_OPT_CACHE_DB => $this->config['db_cache'],
XPDO_OPT_CACHE_PATH => $this->config['lib_path'].'cache/',
XPDO_OPT_HYDRATE_FIELDS => true,
XPDO_OPT_HYDRATE_ADHOC_FIELDS => true,
XPDO_OPT_HYDRATE_RELATED_OBJECTS => true,
XPDO_OPT_TABLE_PREFIX => '',
XPDO_OPT_VALIDATOR_CLASS => 'validation.iltcvalidator',
XPDO_OPT_VALIDATE_ON_SAVE => true,
),$this->config);
$host = $this->config['db_type'] .
':host='.$this->config['db_host'] .
';dbname='.$this->config['db_name'];
parent::__construct(
$host,
$this->config['db_user'],
$this->config['db_pass'],
$this->config,
array(
PDO_ATTR_ERRMODE => PDO_ERRMODE_SILENT,
PDO_ATTR_PERSISTENT => false,
PDO_MYSQL_ATTR_USE_BUFFERED_QUERY => true
));
$this->setPackage('ltc',$this->config['lib_path'].'/model/');
}
...
function getCacheManager$class = 'cache.xPDOCacheManager') {
if (isset($this->config['cache_disabled']) && $this->config['cache_disabled']) {
$this->_cacheEnabled = false;
return null;
}
if ($this->cacheManager === null) {
if ($this->loadClass($class, XPDO_CORE_PATH, true, true)) {
// can override the cache manager, if you want
$cacheManagerClass = isset($this->config['CacheManager.class']) ? $this->config['CacheManager.class'] : 'cache.ltcCacheManager';
if ($className = $this->loadClass($cacheManagerClass, '', false, true)) {
if ($this->cacheManager = new $className($this)) {
$this->_cacheEnabled = true;
}
} else {
$this->_log(XPDO_LOG_LEVEL_FATAL,sprintf($ltc->lexicon['cachemanager_err_nf'],$class));
}
}
}
return $this->cacheManager;
}
}
class MyXPDO extends xPDO
{
function __construct($dsn, $username= '', $password= '', $tablePrefix= '', $driverOptions= null)
{
parent::__construct($dsn, $username, $password, $tablePrefix, $driverOptions);
// set the name of the caching system to use to Memcache.
$this->config = array_merge(array('cache_db_handler' => 'Memcache'), $this->config);
// connect to the cache manager.
if ($cacheManager = $this->getCacheManager())
$cacheManager->objcache->connect('10.0.0.1', 11211);
}
}
// retrieve user and cache their object for half an hour
$user = $xpdo->get('User', array('id' => $modx->getUserLoginID()), 1800);
<?php
class MyXPDO extends xPDO {
function __construct($dsn, $username= '', $password= '', $options= array(), $driverOptions= null) {
// set the name of the caching system to use to Memcache.
$options[XPDO_OPT_DB_CACHE_HANDLER]= 'Memcache';
parent::__construct($dsn, $username, $password, $options, $driverOptions);
// connect to the cache manager.
if ($this->getCacheManager()) {
$this->cacheManager->objcache->connect('10.0.0.1', 11211);
}
}
?>
// snippet to return latest articles
include_once(/path/to/MyXPDO.class.php)
$xpdo = $MyXPDO();
echo getRecentArticles($xpdo, 5);
// params are an xPDO object, and the number of recent articles to return
function getRecentArticles($xpdo, $num)
{
// we can't cache this using xPDO directly since we aren't retrieving an object. So we'll do it ourselves.
// first, check whether there is data in the cache
if($cacheManager = $xpdo->getCacheManager())
{
// creating a key like this means a unique key will be created depending on the parameters the function was called with.
$key = md5('blogs::getRecentArticles: num: '.$num);
// if the html isn't already in the cache, set a flag so we can cache the result after generating it.
if ($html = $cacheManager->get($key))
{
return $html;
}
else
{
$inCache = false;
}
}
// not in the cache, so perform query, then cache the final output
$sql = 'SELECT ...';
// process and format output and put it in $html
$html = ...
if (is_object($cacheManager) && !$inCache)
$cacheManager->set($key, $html, 1800);
return $html;
}
Does there exist a step by step guide to enabling memcache as a caching solution in Revolution?
I see the Cache Handler Class in system setting, this needs to change?