cache() public method

{@inheritDoc}
public cache ( $key, $config = 'default' )
 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;
 }
Example #2
0
 /**
  * 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();
 }
Example #3
0
 /**
  * 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);
 }