public function findCached(Query $query, array $options) { if ($conditions = $query->clause('where')) { $query->cache(function ($q) use($conditions) { return $this->table() . '-' . md5(serialize($conditions)); }); } return $query; }
/** * 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\\Datasource\\ResultSetInterface')); $query->cache('my_key', $cacher)->where(['id' => 1]); $query->all(); }
/** * Integration test for query caching usigna real cache engine and * a formatResults callback * * @return void */ public function testCacheIntegrationWithFormatResults() { $table = TableRegistry::get('Articles'); $query = new Query($this->connection, $table); $cacher = new \Cake\Cache\Engine\FileEngine(); $cacher->init(); $query->select(['id', 'title'])->formatResults(function ($results) { return $results->combine('id', 'title'); })->cache('my_key', $cacher); $expected = $query->toArray(); $query = new Query($this->connection, $table); $results = $query->cache('my_key', $cacher)->toArray(); $this->assertSame($expected, $results); }