From a87ee4927179d0c32587700e0377e9d63b06136c Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 2 Jan 2014 22:39:02 -0500 Subject: [PATCH] Integrate cache writes to Query & add tests.. Once we have a result set we can cache it and return it. Ensure that cache reads and writes happen. Don't write to the cache when we read data from the cache. This might result in infinitely cached data. --- Cake/ORM/Query.php | 3 ++ Cake/ORM/QueryCacher.php | 14 ++++++++ Cake/Test/TestCase/ORM/QueryTest.php | 53 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/Cake/ORM/Query.php b/Cake/ORM/Query.php index f094c50dab7..36a8d470b5b 100644 --- a/Cake/ORM/Query.php +++ b/Cake/ORM/Query.php @@ -517,6 +517,9 @@ public function getResults() { $results = $this->_decorateResults( new ResultSet($this, $this->execute()) ); + if ($this->_cache) { + $this->_cache->store($this, $results); + } } $this->_results = $results; return $this->_results; diff --git a/Cake/ORM/QueryCacher.php b/Cake/ORM/QueryCacher.php index 1b032e65233..eef817379d1 100644 --- a/Cake/ORM/QueryCacher.php +++ b/Cake/ORM/QueryCacher.php @@ -19,6 +19,7 @@ use Cake\Cache\Cache; use Cake\Cache\CacheEngine; use Cake\ORM\Query; +use Cake\ORM\ResultSet; use RuntimeException; /** @@ -64,6 +65,19 @@ public function fetch(Query $query) { 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. * diff --git a/Cake/Test/TestCase/ORM/QueryTest.php b/Cake/Test/TestCase/ORM/QueryTest.php index 930d31de8bc..f21cfb0d2b4 100644 --- a/Cake/Test/TestCase/ORM/QueryTest.php +++ b/Cake/Test/TestCase/ORM/QueryTest.php @@ -1547,4 +1547,57 @@ public function testCacheErrorOnNonSelect() { $query->cache('my_key'); } +/** + * Integration test for query caching. + * + * @return void + */ + public function testCacheReadIntegration() { + $query = $this->getMock( + '\Cake\ORM\Query', ['execute'], + [$this->connection, $this->table] + ); + $resultSet = $this->getMock('\Cake\ORM\ResultSet', [], [$query, null]); + + $query->expects($this->never()) + ->method('execute'); + + $cacher = $this->getMock('Cake\Cache\CacheEngine'); + $cacher->expects($this->once()) + ->method('read') + ->with('my_key') + ->will($this->returnValue($resultSet)); + + $query->cache('my_key', $cacher) + ->where(['id' => 1]); + + $results = $query->all(); + $this->assertSame($resultSet, $results); + } + +/** + * Integration test for query caching. + * + * @return void + */ + public function testCacheWriteIntegration() { + $table = TableRegistry::get('Articles'); + $query = new Query($this->connection, $table); + + $query->select(['id', 'title']); + + $cacher = $this->getMock('Cake\Cache\CacheEngine'); + $cacher->expects($this->once()) + ->method('write') + ->with( + 'my_key', + $this->isInstanceOf('Cake\ORM\ResultSet') + ); + + $query->cache('my_key', $cacher) + ->where(['id' => 1]); + + $query->all(); + } + }