From 762ebd4b939d510720590d34ce29bda858eb7d0d Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 17 Jul 2011 19:08:23 -0430 Subject: [PATCH] Implementing a persitent method cache for DboSource, using a stronger hashing algorithm to ensure unique keys --- lib/Cake/Model/Datasource/DboSource.php | 34 ++++++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 66786e9684f..17080bc7916 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -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 * @@ -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; } @@ -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; } @@ -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) { @@ -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_'); + } + } + }