Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2599 from markstory/3.0-query-cache
3.0 make query caching simpler
- Loading branch information
Showing
6 changed files
with
389 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ | |
* ] | ||
* ] | ||
* }}} | ||
* | ||
*/ | ||
class CounterCacheBehavior extends Behavior { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\ORM; | ||
|
||
use Cake\Cache\Cache; | ||
use Cake\Cache\CacheEngine; | ||
use Cake\ORM\Query; | ||
use Cake\ORM\ResultSet; | ||
use RuntimeException; | ||
|
||
/** | ||
* Handles caching queries and loading results from the cache. | ||
* | ||
* Used by Cake\ORM\Query internally. | ||
* | ||
* @see Cake\ORM\Query::cache() for the public interface. | ||
*/ | ||
class QueryCacher { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string|Closure $key | ||
* @param string|CacheEngine $config | ||
*/ | ||
public function __construct($key, $config) { | ||
if (!is_string($key) && !is_callable($key)) { | ||
throw new RuntimeException('Cache keys must be strings or callables.'); | ||
} | ||
$this->_key = $key; | ||
|
||
if (!is_string($config) && !($config instanceof CacheEngine)) { | ||
throw new RuntimeException('Cache configs must be strings or CacheEngine instances.'); | ||
} | ||
$this->_config = $config; | ||
} | ||
|
||
/** | ||
* Load the cached results from the cache or run the query. | ||
* | ||
* @param Query $query The query the cache read is for. | ||
* @return ResultSet|null Either the cached results or null. | ||
*/ | ||
public function fetch(Query $query) { | ||
$key = $this->_resolveKey($query); | ||
$storage = $this->_resolveCacher(); | ||
$result = $storage->read($key); | ||
if (empty($result)) { | ||
return null; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Store the result set into the cache. | ||
* | ||
* @param Query $query The query the cache read is for. | ||
* @param ResultSet The result set to store. | ||
* @return void | ||
*/ | ||
public function store(Query $query, ResultSet $results) { | ||
$key = $this->_resolveKey($query); | ||
$storage = $this->_resolveCacher(); | ||
return $storage->write($key, $results); | ||
} | ||
|
||
/** | ||
* Get/generate the cache key. | ||
* | ||
* @param Query $query | ||
* @return string | ||
*/ | ||
protected function _resolveKey($query) { | ||
if (is_string($this->_key)) { | ||
return $this->_key; | ||
} | ||
$func = $this->_key; | ||
$key = $func($query); | ||
if (!is_string($key)) { | ||
$msg = sprintf('Cache key functions must return a string. Got %s.', var_export($key, true)); | ||
throw new RuntimeException($msg); | ||
} | ||
return $key; | ||
} | ||
|
||
/** | ||
* Get the cache engine. | ||
* | ||
* @return Cake\Cache\CacheEngine | ||
*/ | ||
protected function _resolveCacher() { | ||
if (is_string($this->_config)) { | ||
return Cache::engine($this->_config); | ||
} | ||
return $this->_config; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* PHP Version 5.4 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\TestCase\ORM; | ||
|
||
use Cake\Cache\Cache; | ||
use Cake\ORM\QueryCacher; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* Query cacher test | ||
*/ | ||
class QueryCacherTest extends TestCase { | ||
|
||
/** | ||
* Setup method | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->engine = $this->getMock('Cake\Cache\CacheEngine'); | ||
$this->engine->expects($this->any()) | ||
->method('init') | ||
->will($this->returnValue(true)); | ||
|
||
Cache::config('queryCache', $this->engine); | ||
} | ||
|
||
/** | ||
* Teardown method | ||
* | ||
* @return void | ||
*/ | ||
public function tearDown() { | ||
parent::tearDown(); | ||
Cache::drop('queryCache'); | ||
} | ||
|
||
/** | ||
* Test fetching with a function to generate the key. | ||
* | ||
* @return void | ||
*/ | ||
public function testFetchFunctionKey() { | ||
$this->_mockRead('my_key', 'A winner'); | ||
$query = $this->getMock('Cake\ORM\Query', [], [], '', false); | ||
|
||
$cacher = new QueryCacher(function($q) use ($query) { | ||
$this->assertSame($query, $q); | ||
return 'my_key'; | ||
}, 'queryCache'); | ||
|
||
$result = $cacher->fetch($query); | ||
$this->assertEquals('A winner', $result); | ||
} | ||
|
||
/** | ||
* Test fetching with a function to generate the key but the function is poop. | ||
* | ||
* @expectedException \RuntimeException | ||
* @expectedExceptionMessage Cache key functions must return a string. Got false. | ||
* @return void | ||
*/ | ||
public function testFetchFunctionKeyNoString() { | ||
$this->_mockRead('my_key', 'A winner'); | ||
$query = $this->getMock('Cake\ORM\Query', [], [], '', false); | ||
|
||
$cacher = new QueryCacher(function($q) { | ||
return false; | ||
}, 'queryCache'); | ||
|
||
$cacher->fetch($query); | ||
} | ||
|
||
/** | ||
* Test fetching with a cache instance. | ||
* | ||
* @return void | ||
*/ | ||
public function testFetchCacheHitStringEngine() { | ||
$this->_mockRead('my_key', 'A winner'); | ||
$cacher = new QueryCacher('my_key', 'queryCache'); | ||
$query = $this->getMock('Cake\ORM\Query', [], [], '', false); | ||
$result = $cacher->fetch($query); | ||
$this->assertEquals('A winner', $result); | ||
} | ||
|
||
/** | ||
* Test fetching with a cache hit. | ||
* | ||
* @return void | ||
*/ | ||
public function testFetchCacheHit() { | ||
$this->_mockRead('my_key', 'A winner'); | ||
$cacher = new QueryCacher('my_key', $this->engine); | ||
$query = $this->getMock('Cake\ORM\Query', [], [], '', false); | ||
$result = $cacher->fetch($query); | ||
$this->assertEquals('A winner', $result); | ||
} | ||
|
||
/** | ||
* Test fetching with a cache miss. | ||
* | ||
* @return void | ||
*/ | ||
public function testFetchCacheMiss() { | ||
$this->_mockRead('my_key', false); | ||
$cacher = new QueryCacher('my_key', $this->engine); | ||
$query = $this->getMock('Cake\ORM\Query', [], [], '', false); | ||
$result = $cacher->fetch($query); | ||
$this->assertNull($result, 'Cache miss should not have an isset() return.'); | ||
} | ||
|
||
/** | ||
* Helper for building mocks. | ||
*/ | ||
protected function _mockRead($key, $value = false) { | ||
$this->engine->expects($this->any()) | ||
->method('read') | ||
->with($key) | ||
->will($this->returnValue($value)); | ||
} | ||
|
||
} |
Oops, something went wrong.