For lengthy operations I create a method in the model and cache the
results. Say I have sale people whose main portal page contains action
items, deals about to close, closed deals, and current prospect status.
This data is referenced slightly different ways in various parts of
the app. Also, this main page is shown repeatedly through the session
when the user is navigating. Parts of the data can update alot, others
rarely.
so closed deals changes rarely (less than once a day) but the query is
real harsh:
class Revenue extends AppModel {
function getRevenue( $country, $region, $year ) {
$cache =& $this->getCache(); // constructs Cache_Lite object
$cachedata = $cache->get("$country-$region-$year",'revenue');
if ($cachedata === false) {
$revenue = array();
// do big query here ....
// populate $revenue with results optimized for reuse
$cache->save( serialize($revenue) );
} else {
$revenue = unserialize($cachedata);
}
// allow getCache to not reconstruct
// if needed again soon
$this->releaseCache($cache);
return $revenue;
}
}
Part of my AppModel:
define('_APPMODEL_CACHE_DEFAULT_TIME_', HOUR * 15);
class AppModel extends Model{
var $_cache = null;
const CACHE_DEFAULT_TIME = _APPMODEL_CACHE_DEFAULT_TIME_;
function & getCache() {
if (!$this->_cache) {
$cache = $this->createCache();
} else {
$cache = $this->_cache;
}
$this->_cache = null;
return $cache;
}
function & createCache($lifetime=AppModel::CACHE_DEFAULT_TIME) {
$options = array(
'cacheDir' => APP.'tmp/cache/persistent/',
'lifeTime' => $lifetime,
'pearErrorMode' => CACHE_LITE_ERROR_DIE,
'fileNameProtection' => false
);
$cache = new Cache_Lite($options);
return $cache;
}
function releaseCache(&$cache) {
$this->_cache = & $cache;
}
}
and you can do the same with other data on the page. ALL of my simple
lookups that are database tables that change rarely are cached and
retrieved this way so no form has to do constant DB lookups just to get
a list of product codes, countries, whatever. Stay off of your
database as much as possible and yor scalability soars.
HTH,
David
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---