Skip to content

Commit

Permalink
Implementing a persitent method cache for DboSource, using a stronger…
Browse files Browse the repository at this point in the history
… hashing algorithm to ensure unique keys
  • Loading branch information
lorenzo committed Jul 17, 2011
1 parent ca0a7e4 commit 762ebd4
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -246,6 +246,14 @@ class DboSource extends DataSource {
*/
public $fieldParameters = array();

/**
* Indicates whether there was a change on the cached results on the methods of this class
* This will be used for storing in a more persistent cache
*
* @var boolean
*/
protected $_methodCacheChange = false;

/**
* Constructor
*
Expand Down Expand Up @@ -764,9 +772,13 @@ public function cacheMethod($method, $key, $value = null) {
if ($this->cacheMethods === false) {
return $value;
}
if (empty($this->methodCache)) {
$this->methodCache = Cache::read('method_cache', '_cake_core_');
}
if ($value === null) {
return (isset($this->methodCache[$method][$key])) ? $this->methodCache[$method][$key] : null;
}
$this->_methodCacheChange = true;
return $this->methodCache[$method][$key] = $value;
}

Expand Down Expand Up @@ -2167,17 +2179,15 @@ public function fields($model, $alias = null, $fields = array(), $quote = true)
if (empty($alias)) {
$alias = $model->alias;
}
$virtualFields = $model->getVirtualField();
$cacheKey = array(
$model->useDbConfig,
$model->table,
array_keys($model->schema()),
$model->name,
$model->getVirtualField(),
$alias,
$model->alias,
$virtualFields,
$fields,
$quote
);
$cacheKey = crc32(serialize($cacheKey));
$cacheKey = md5(serialize($cacheKey));
if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
return $return;
}
Expand All @@ -2191,7 +2201,6 @@ public function fields($model, $alias = null, $fields = array(), $quote = true)
$allFields = $allFields || in_array('*', $fields) || in_array($model->alias . '.*', $fields);

$virtual = array();
$virtualFields = $model->getVirtualField();
if (!empty($virtualFields)) {
$virtualKeys = array_keys($virtualFields);
foreach ($virtualKeys as $field) {
Expand Down Expand Up @@ -3123,4 +3132,15 @@ public function getQueryCache($sql, $params = array()) {
return false;
}

/**
* Used for storing in cache the results of the in-memory methodCache
*
* @return void
*/
public function __destruct() {
if ($this->_methodCacheChange) {
Cache::write('method_cache', $this->methodCache, '_cake_core_');
}
}

}

0 comments on commit 762ebd4

Please sign in to comment.