Skip to content
Permalink
Browse files

Caching schema metadata based on Connection configurations.

  • Loading branch information...
renan committed Dec 25, 2013
1 parent 7cd3349 commit f1399f3dd341e3831e19c35b37d7f0a687fce71e
Showing with 50 additions and 0 deletions.
  1. +17 −0 Cake/Database/Schema/Collection.php
  2. +28 −0 Cake/Test/TestCase/Database/Schema/CollectionTest.php
  3. +5 −0 Cake/Test/init.php
@@ -16,6 +16,7 @@
*/
namespace Cake\Database\Schema;
use Cake\Cache\Cache;
use Cake\Database\Connection;
use Cake\Database\Exception;
use Cake\Database\Schema\Table;
@@ -71,13 +72,25 @@ public function listTables() {
/**
* Get the column metadata for a table.
*
* Caching will be applied if `cacheMetadata` key is present in the Connection
* configuration options. Defaults to _cake_model_ when true.
*
* @param string $name The name of the table to describe.
* @return Cake\Database\Schema\Table Object with column metadata.
* @throws Cake\Database\Exception when table cannot be described.
*/
public function describe($name) {
$config = $this->_connection->config();
if (!empty($config['cacheMetadata'])) {
$cacheConfig = ($config['cacheMetadata'] === true) ? '_cake_model_' : $config['cacheMetadata'];
$cacheKey = $this->_connection->configName() . '_' . $name;
$cached = Cache::read($cacheKey, $cacheConfig);
if ($cached !== false) {
return $cached;
}
}
list($sql, $params) = $this->_dialect->describeTableSql($name, $config);
$statement = $this->_executeSql($sql, $params);
if (count($statement) === 0) {
@@ -101,6 +114,10 @@ public function describe($name) {
$this->_dialect->convertForeignKeyDescription($table, $row);
}
$statement->closeCursor();
if (!empty($config['cacheMetadata'])) {
Cache::write($cacheKey, $table, $cacheConfig);
}
return $table;
}
@@ -16,7 +16,9 @@
*/
namespace Cake\Test\TestCase\Database\Schema;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\ConnectionManager;
use Cake\Database\Schema\Collection;
use Cake\Database\Schema\Table;
@@ -27,6 +29,10 @@
*/
class CollectionTest extends TestCase {
public $fixtures = [
'core.user'
];
/**
* Setup function
*
@@ -61,4 +67,26 @@ public function testDescribeIncorrectTable() {
$this->assertNull($schema->describe('derp'));
}
/**
* Tests that schema metadata is cached
*
* @return void
*/
public function testDescribeCache() {
$table = $this->connection->schemaCollection()->describe('users');
$config = $this->connection->config();
$config['cacheMetadata'] = true;
$connection = new Connection($config);
$schema = new Collection($connection);
Cache::delete('test_users', '_cake_model_');
$result = $schema->describe('users');
$this->assertEquals($table, $result);
$result = Cache::read('test_users', '_cake_model_');
$this->assertEquals($table, $result);
}
}
@@ -84,6 +84,11 @@
'engine' => 'File',
'prefix' => 'cake_core_',
'serialize' => true
],
'_cake_model_' => [
'engine' => 'File',
'prefix' => 'cake_model_',
'serialize' => true
]
]);

0 comments on commit f1399f3

Please sign in to comment.
You can’t perform that action at this time.