Skip to content

Commit

Permalink
Merge pull request #4816 from sukihub/3.0-cacheable-get-v1
Browse files Browse the repository at this point in the history
3.0 - Query caching for \Cake\ORM\Table::get (updated)
  • Loading branch information
markstory committed Oct 9, 2014
2 parents 0b32554 + b62d965 commit b2f9bbf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,24 @@ public function get($primaryKey, $options = []) {
));
}
$conditions = array_combine($key, $primaryKey);
$entity = $this->find('all', $options)->where($conditions)->first();

$cacheConfig = isset($options['cache']) ? $options['cache'] : false;
$cacheKey = isset($options['key']) ? $options['key'] : false;
unset($options['key'], $options['cache']);

$query = $this->find('all', $options)->where($conditions);

if ($cacheConfig) {
if (!$cacheKey) {
$cacheKey = sprintf(
"get:%s.%s%s",
$this->connection()->configName(), $this->table(), json_encode($primaryKey)
);
}
$query->cache($cacheKey, $cacheConfig);
}

$entity = $query->first();

if ($entity) {
return $entity;
Expand Down
78 changes: 75 additions & 3 deletions tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3301,13 +3301,81 @@ public function testSimplifiedFind() {
$table->find();
}

public function providerForTestGet() {
return [
[ ['fields' => ['id']] ],
[ ['fields' => ['id'], 'cache' => false] ]
];
}

/**
* Test that get() will use the primary key for searching and return the first
* entity found
*
* @dataProvider providerForTestGet
* @param array $options
* @return void
*/
public function testGet($options) {
$table = $this->getMock(
'\Cake\ORM\Table',
['callFinder', 'query'],
[[
'connection' => $this->connection,
'schema' => [
'id' => ['type' => 'integer'],
'bar' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['bar']]]
]
]]
);

$query = $this->getMock(
'\Cake\ORM\Query',
['addDefaultTypes', 'first', 'where', 'cache'],
[$this->connection, $table]
);

$entity = new \Cake\ORM\Entity;
$table->expects($this->once())->method('query')
->will($this->returnValue($query));
$table->expects($this->once())->method('callFinder')
->with('all', $query, ['fields' => ['id']])
->will($this->returnValue($query));

$query->expects($this->once())->method('where')
->with([$table->alias() . '.bar' => 10])
->will($this->returnSelf());
$query->expects($this->never())->method('cache');
$query->expects($this->once())->method('first')
->will($this->returnValue($entity));
$result = $table->get(10, $options);
$this->assertSame($entity, $result);
}

public function providerForTestGetWithCache() {
return [
[
['fields' => ['id'], 'cache' => 'default'],
'get:test.table_name[10]', 'default'
],
[
['fields' => ['id'], 'cache' => 'default', 'key' => 'custom_key'],
'custom_key', 'default'
]
];
}

/**
* Test that get() will use the cache.
*
* @dataProvider providerForTestGetWithCache
* @param array $options
* @param string $cacheKey
* @param string $cacheConfig
* @return void
*/
public function testGet() {
public function testGetWithCache($options, $cacheKey, $cacheConfig) {
$table = $this->getMock(
'\Cake\ORM\Table',
['callFinder', 'query'],
Expand All @@ -3320,10 +3388,11 @@ public function testGet() {
]
]]
);
$table->table('table_name');

$query = $this->getMock(
'\Cake\ORM\Query',
['addDefaultTypes', 'first', 'where'],
['addDefaultTypes', 'first', 'where', 'cache'],
[$this->connection, $table]
);

Expand All @@ -3337,9 +3406,12 @@ public function testGet() {
$query->expects($this->once())->method('where')
->with([$table->alias() . '.bar' => 10])
->will($this->returnSelf());
$query->expects($this->once())->method('cache')
->with($cacheKey, $cacheConfig)
->will($this->returnSelf());
$query->expects($this->once())->method('first')
->will($this->returnValue($entity));
$result = $table->get(10, ['fields' => ['id']]);
$result = $table->get(10, $options);
$this->assertSame($entity, $result);
}

Expand Down

0 comments on commit b2f9bbf

Please sign in to comment.