Skip to content

Commit

Permalink
Add orm_cache clear command.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 20, 2014
1 parent 9686448 commit 038f1fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Console/Command/OrmCacheShell.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Console\Command;

use Cake\Cache\Cache;
use Cake\Console\Shell;
use Cake\Datasource\ConnectionManager;

Expand Down Expand Up @@ -56,6 +57,7 @@ public function build($name = null) {
$this->_io->verbose('Building metadata cache for ' . $table);
$schema->describe($table);
}
$this->out('<success>Cache build complete</success>');
}

/**
Expand All @@ -66,6 +68,27 @@ public function build($name = null) {
*/
public function clear($name = null) {
$source = ConnectionManager::get($this->params['connection']);
$schema = $source->schemaCollection();
$tables = [$name];
if (empty($name)) {
$tables = $schema->listTables();
}
if (!$schema->cacheMetadata()) {
$this->_io->verbose('Metadata cache was disabled in config. Enabling to clear cache.');
$schema->cacheMetadata(true);
}
$configName = $schema->cacheMetadata();

foreach ($tables as $table) {
$this->_io->verbose(sprintf(
'Clearing metadata cache from "%s" for %s',
$configName,
$table
));
$key = $schema->cacheKey($table);
Cache::delete($key, $configName);
}
$this->out('<success>Cache clear complete</success>');
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/Database/Schema/Collection.php
Expand Up @@ -93,7 +93,7 @@ public function listTables() {
public function describe($name) {
$cacheConfig = $this->cacheMetadata();
if ($cacheConfig) {
$cacheKey = $this->_connection->configName() . '_' . $name;
$cacheKey = $this->cacheKey($name);
$cached = Cache::read($cacheKey, $cacheConfig);
if ($cached !== false) {
return $cached;
Expand Down Expand Up @@ -131,6 +131,16 @@ public function describe($name) {
return $table;
}

/**
* Get the cache key for a given name.
*
* @param string $name The name to get a cache key for.
* @return string The cache key.
*/
public function cacheKey($name) {
return $this->_connection->configName() . '_' . $name;
}

/**
* Sets the cache config name to use for caching table metadata, or
* disabels it if false is passed.
Expand Down

0 comments on commit 038f1fb

Please sign in to comment.