Skip to content

Commit

Permalink
Change cacheMethodFilters to be a method
Browse files Browse the repository at this point in the history
  • Loading branch information
tersmitten committed Nov 14, 2016
1 parent 58cc9b4 commit 71535d2
Showing 1 changed file with 15 additions and 43 deletions.
58 changes: 15 additions & 43 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -60,49 +60,13 @@ class DboSource extends DataSource {
public static $methodCache = array();

/**
* Whether or not to cache the results of DboSource::name(), DboSource::fields() and DboSource::conditions()
* Whether or not to cache the results of DboSource::name() 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
Expand Down Expand Up @@ -822,18 +786,26 @@ 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)) {
if (!$this->cacheMethodFilter($method, $key, $value)) {
return $value;
}

$this->_methodCacheChange = true;
return static::$methodCache[$method][$key] = $value;
}

/**
* Filters to apply to the results of `name` and `fields`. When the filter for a given method does not return `true`
* then the result is not added to the memory cache.
*
* @param string $method Name of the method being cached.
* @param string $key The key name for the cache operation.
* @param mixed $value The value to cache into memory.
* @return bool Whether or not to cache
*/
public function cacheMethodFilter($method, $key, $value) {
return true;
}

/**
* Returns a quoted name of $data for use in an SQL statement.
* Strips fields out of SQL functions before quoting.
Expand Down

0 comments on commit 71535d2

Please sign in to comment.