From 58cc9b4596658226a4750c5b1dc4ea25f2daffc4 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Fri, 11 Nov 2016 23:37:23 +0100 Subject: [PATCH] Make it possible to filter values per method in DboSource::cacheMethod --- lib/Cake/Model/Datasource/DboSource.php | 46 ++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index a73eea8a720..846f932d31b 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -60,13 +60,49 @@ class DboSource extends DataSource { public static $methodCache = array(); /** - * Whether or not to cache the results of DboSource::name() and DboSource::conditions() + * Whether or not to cache the results of DboSource::name(), DboSource::fields() and DboSource::conditions() * into the memory cache. Set to false to disable the use of the memory cache. * * @var bool */ public $cacheMethods = true; +/** + * Filters to apply to the results of DboSource::name(), DboSource::fields() and DboSource::conditions(). + * When the filter function for a given method does not return true then the result is not added to the memory cache. + * + * For instance: + * + * ``` + * array( + * // For method fields, do not cache values that contain floats + * 'fields' => function ($value) { + * $hasFloat = preg_grep('/(\d+)?\.\d+/', $value); + * + * return count($hasFloat) === 0; + * }, + * // For method name, do not cache values that have the name floats + * 'name' => function ($value) { + * return preg_match('/^`rating_diff`$/', $value) !== 1; + * } + * ) + * ``` + * + * or + * + * ``` + * array( + * // For method fields, do not cache any values + * 'fields' => function () { + * return false; + * }, + * ) + * ``` + * + * @var array + */ + public $cacheMethodFilters = array(); + /** * Flag to support nested transactions. If it is set to false, you will be able to use * the transaction methods (begin/commit/rollback), but just the global transaction will @@ -786,6 +822,14 @@ public function cacheMethod($method, $key, $value = null) { if ($value === null) { return (isset(static::$methodCache[$method][$key])) ? static::$methodCache[$method][$key] : null; } + + $methodFilterExists = ( + isset($this->cacheMethodFilters[$method]) && is_callable($this->cacheMethodFilters[$method]) + ); + if ($methodFilterExists && !call_user_func($this->cacheMethodFilters[$method], $value)) { + return $value; + } + $this->_methodCacheChange = true; return static::$methodCache[$method][$key] = $value; }