src/Model/Behavior/CacheableBehavior.php
public function beforeFind(Model $model, $query)
    {
        if (Configure::read('Cache.disable') || !isset($query['cache'])) {
            $this->_isCaching = false;

            return true;
        }

        // Grab the cache key and expiration
        $key = $query['cache'];
        $expires = isset($query['cacheExpires']) ? $query['cacheExpires'] : null;
        $forceRefresh = isset($query['cacheForceRefresh']) ? $query['cacheForceRefresh'] : false;

        if ($key === true) {
            $key = array($model->alias, md5(json_encode($query)));

        } else if (is_array($key)) {
            if (isset($key['expires'])) {
                $expires = $key['expires'];
            }

            if (isset($key['key'])) {
                $key = $key['key'];
            }
        }

        $key = $this->cacheKey($model, $key, false);
        $expires = $this->getExpiration($model, $expires);

        $this->_isCaching = true;
        $this->_currentQuery = array(
            'key' => $key,
            'expires' => $expires,
            'forceRefresh' => $forceRefresh
        );

        // Are results already cached?
        $results = null;

        if (!$forceRefresh) {
            if (!empty($this->_cached[$key])) {
                $results = $this->_cached[$key];

            } else if ($fromCache = $this->readCache($model, $key)) {
                $results = $fromCache;
            }
        }

        // Begin caching by replacing with ShimSource
        if ($results) {
            $this->_cached[$key] = $results;
            $this->_previousDbConfig = $model->useDbConfig;

            // Create DataSource config if it doesn't exist
            $dbConfig = $this->settings[$model->alias]['dbConfig'];

            ConnectionManager::create($dbConfig, array(
                'datasource' => 'Utility.ShimSource',
                'database' => null
            ));

            $model->useDbConfig = $dbConfig;
        }

        return true;
    }
public function beforeFind(Model $model, $query)
    {
        if (Configure::read('Cache.disable') || !isset($query['cache'])) {
            $this->_isCaching = false;

            return true;
        }

        // Grab the cache key and expiration
        $key = $query['cache'];
        $expires = isset($query['cacheExpires']) ? $query['cacheExpires'] : null;
        $forceRefresh = isset($query['cacheForceRefresh']) ? $query['cacheForceRefresh'] : false;

        if ($key === true) {
            $key = array($model->alias, md5(json_encode($query)));

        } else if (is_array($key)) {
            if (isset($key['expires'])) {
                $expires = $key['expires'];
            }

            if (isset($key['key'])) {
                $key = $key['key'];
            }
        }

        $key = $this->cacheKey($model, $key, false);
        $expires = $this->getExpiration($model, $expires);

        $this->_isCaching = true;
        $this->_currentQuery = array(
            'key' => $key,
            'expires' => $expires,
            'forceRefresh' => $forceRefresh
        );

        // Are results already cached?
        $results = null;

        if (!$forceRefresh) {
            if (!empty($this->_cached[$key])) {
                $results = $this->_cached[$key];

            } else if ($fromCache = $this->readCache($model, $key)) {
                $results = $fromCache;
            }
        }

        // Begin caching by replacing with ShimSource
        if ($results) {
            $this->_cached[$key] = $results;
            $this->_previousDbConfig = $model->useDbConfig;

            // Create DataSource config if it doesn't exist
            $dbConfig = $this->settings[$model->alias]['dbConfig'];

            ConnectionManager::create($dbConfig, array(
                'datasource' => 'Utility.ShimSource',
                'database' => null
            ));

            $model->useDbConfig = $dbConfig;
        }

        return true;
    }
public function beforeFind ( Model $ model , $ query ) { if ( Configure :: read ( 'Cache.disable' ) || ! isset ( $ query [ 'cache' ] ) ) { $ this -> _isCaching = false ; return true ; } // Grab the cache key and expiration
 $ key = $ query [ 'cache' ] ; $ expires = isset ( $ query [ 'cacheExpires' ] ) ? $ query [ 'cacheExpires' ] : null ; $ forceRefresh = isset ( $ query [ 'cacheForceRefresh' ] ) ? $ query [ 'cacheForceRefresh' ] : false ; if ( $ key === true ) { $ key = array ( $ model -> alias , md5 ( json_encode ( $ query ) ) ) ; } else if ( is_array ( $ key ) ) { if ( isset ( $ key [ 'expires' ] ) ) { $ expires = $ key [ 'expires' ] ; } if ( isset ( $ key [ 'key' ] ) ) { $ key = $ key [ 'key' ] ; } } $ key = $ this -> cacheKey ( $ model , $ key , false ) ; $ expires = $ this -> getExpiration ( $ model , $ expires ) ; $ this -> _isCaching = true ; $ this -> _currentQuery = array ( 'key' => $ key , 'expires' => $ expires , 'forceRefresh' => $ forceRefresh ) ; // Are results already cached?
 $ results = null ; if ( ! $ forceRefresh ) { if ( ! empty ( $ this -> _cached [ $ key ] ) ) { $ results = $ this -> _cached [ $ key ] ; } else if ( $ fromCache = $ this -> readCache ( $ model , $ key ) ) { $ results = $ fromCache ; } } // Begin caching by replacing with ShimSource
 if ( $ results ) { $ this -> _cached [ $ key ] = $ results ; $ this -> _previousDbConfig = $ model -> useDbConfig ; // Create DataSource config if it doesn't exist
 $ dbConfig = $ this -> settings [ $ model -> alias ] [ 'dbConfig' ] ; ConnectionManager :: create ( $ dbConfig , array ( 'datasource' => 'Utility.ShimSource' , 'database' => null ) ) ; $ model -> useDbConfig = $ dbConfig ; } return true ; }
Before a query is executed, look for the cache parameter.
If the cache param exists, generate a cache key and fetch the results from the cache.
If the result is empty or the cache doesn't exist, replace the current datasource
with a dummy shim datasource, allowing us to pull in cached results.

@param Model $model
@param array $query
@return array|boolean
Before a query is executed look for the cache parameter . If the cache param exists generate a cache key and fetch the results from the cache . If the result is empty or the cache doesn t exist replace the current datasource with a dummy shim datasource allowing us to pull in cached results .