Browse Source

Add `orm_cache clear` command.

mark_story 12 years ago
parent
commit
038f1fb3b0
2 changed files with 34 additions and 1 deletions
  1. 23 0
      src/Console/Command/OrmCacheShell.php
  2. 11 1
      src/Database/Schema/Collection.php

+ 23 - 0
src/Console/Command/OrmCacheShell.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Console\Command;
 
+use Cake\Cache\Cache;
 use Cake\Console\Shell;
 use Cake\Datasource\ConnectionManager;
 
@@ -56,6 +57,7 @@ class OrmCacheShell extends Shell {
 			$this->_io->verbose('Building metadata cache for ' . $table);
 			$schema->describe($table);
 		}
+		$this->out('<success>Cache build complete</success>');
 	}
 
 /**
@@ -66,6 +68,27 @@ class OrmCacheShell extends Shell {
  */
 	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>');
 	}
 
 /**

+ 11 - 1
src/Database/Schema/Collection.php

@@ -93,7 +93,7 @@ class Collection {
 	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;
@@ -132,6 +132,16 @@ class Collection {
 	}
 
 /**
+ * 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.
  * If called with no arguments it returns the current configuration name.