Skip to content

Commit

Permalink
Cache option for \Cake\ORM\Table::get
Browse files Browse the repository at this point in the history
more unique cache key,
improved API
  • Loading branch information
Jan Sukenik committed Oct 6, 2014
1 parent 3f6fefc commit b62d965
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
21 changes: 13 additions & 8 deletions src/ORM/Table.php
Expand Up @@ -925,18 +925,23 @@ public function get($primaryKey, $options = []) {
}
$conditions = array_combine($key, $primaryKey);

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

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

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

$entity = $query->where($conditions)->first();
$entity = $query->first();

if ($entity) {
return $entity;
Expand Down
37 changes: 32 additions & 5 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -3304,13 +3304,22 @@ 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() {
public function testGet($options) {
$table = $this->getMock(
'\Cake\ORM\Table',
['callFinder', 'query'],
Expand Down Expand Up @@ -3343,16 +3352,33 @@ public function testGet() {
$query->expects($this->never())->method('cache');
$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);
}

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 testGetWithCache() {
public function testGetWithCache($options, $cacheKey, $cacheConfig) {
$table = $this->getMock(
'\Cake\ORM\Table',
['callFinder', 'query'],
Expand All @@ -3365,6 +3391,7 @@ public function testGetWithCache() {
]
]]
);
$table->table('table_name');

$query = $this->getMock(
'\Cake\ORM\Query',
Expand All @@ -3383,11 +3410,11 @@ public function testGetWithCache() {
->with([$table->alias() . '.bar' => 10])
->will($this->returnSelf());
$query->expects($this->once())->method('cache')
->with($table->table() . '_[10]', 'any_cache')
->with($cacheKey, $cacheConfig)
->will($this->returnSelf());
$query->expects($this->once())->method('first')
->will($this->returnValue($entity));
$result = $table->get(10, ['fields' => ['id'], 'cache' => 'any_cache']);
$result = $table->get(10, $options);
$this->assertSame($entity, $result);
}

Expand Down

0 comments on commit b62d965

Please sign in to comment.