_schema = $this->getSchema($connection); } /** * Build metadata. * * @param string|null $name The name of the table to build cache data for. * @return array Returns a list build table caches */ public function build($name = null) { $tables = [$name]; if (empty($name)) { $tables = $this->_schema->listTables(); } foreach ($tables as $table) { $this->_schema->describe($table, ['forceRefresh' => true]); } return $tables; } /** * Clear metadata. * * @param string|null $name The name of the table to clear cache data for. * @return array Returns a list of cleared table caches */ public function clear($name = null) { $tables = [$name]; if (empty($name)) { $tables = $this->_schema->listTables(); } $configName = $this->_schema->getCacheMetadata(); foreach ($tables as $table) { $key = $this->_schema->cacheKey($table); Cache::delete($key, $configName); } return $tables; } /** * Helper method to get the schema collection. * * @param \Cake\Database\Connection $connection Connection object * @return \Cake\Database\Schema\Collection|\Cake\Database\Schema\CachedCollection * @throws \RuntimeException If given connection object is not compatible with schema caching */ public function getSchema(Connection $connection) { if (!method_exists($connection, 'schemaCollection')) { throw new RuntimeException('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.'); } $config = $connection->config(); if (empty($config['cacheMetadata'])) { $connection->cacheMetadata(true); } return $connection->getSchemaCollection(); } }