OrmCacheShell.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell;
  16. use Cake\Cache\Cache;
  17. use Cake\Console\Shell;
  18. use Cake\Datasource\ConnectionManager;
  19. /**
  20. * ORM Cache Shell.
  21. *
  22. * Provides a CLI interface to the ORM metadata caching features.
  23. * This tool is intended to be used by deployment scripts so that you
  24. * can prevent thundering herd effects on the metadata cache when new
  25. * versions of your application are deployed, or when migrations
  26. * requiring updated metadata are required.
  27. */
  28. class OrmCacheShell extends Shell
  29. {
  30. /**
  31. * Build metadata.
  32. *
  33. * @param string|null $name The name of the table to build cache data for.
  34. * @return bool
  35. */
  36. public function build($name = null)
  37. {
  38. $schema = $this->_getSchema();
  39. if (!$schema) {
  40. return false;
  41. }
  42. $tables = [$name];
  43. if (empty($name)) {
  44. $tables = $schema->listTables();
  45. }
  46. foreach ($tables as $table) {
  47. $this->_io->verbose('Building metadata cache for ' . $table);
  48. $schema->describe($table, ['forceRefresh' => true]);
  49. }
  50. $this->out('<success>Cache build complete</success>');
  51. return true;
  52. }
  53. /**
  54. * Clear metadata.
  55. *
  56. * @param string|null $name The name of the table to clear cache data for.
  57. * @return bool
  58. */
  59. public function clear($name = null)
  60. {
  61. $schema = $this->_getSchema();
  62. if (!$schema) {
  63. return false;
  64. }
  65. $tables = [$name];
  66. if (empty($name)) {
  67. $tables = $schema->listTables();
  68. }
  69. $configName = $schema->cacheMetadata();
  70. foreach ($tables as $table) {
  71. $this->_io->verbose(sprintf(
  72. 'Clearing metadata cache from "%s" for %s',
  73. $configName,
  74. $table
  75. ));
  76. $key = $schema->cacheKey($table);
  77. Cache::delete($key, $configName);
  78. }
  79. $this->out('<success>Cache clear complete</success>');
  80. return true;
  81. }
  82. /**
  83. * Helper method to get the schema collection.
  84. *
  85. * @return false|\Cake\Database\Schema\Collection
  86. */
  87. protected function _getSchema()
  88. {
  89. $source = ConnectionManager::get($this->params['connection']);
  90. if (!method_exists($source, 'schemaCollection')) {
  91. $msg = sprintf(
  92. 'The "%s" connection is not compatible with orm caching, ' .
  93. 'as it does not implement a "schemaCollection()" method.',
  94. $this->params['connection']
  95. );
  96. $this->error($msg);
  97. return false;
  98. }
  99. $config = $source->config();
  100. if (empty($config['cacheMetadata'])) {
  101. $this->_io->verbose('Metadata cache was disabled in config. Enabling to clear cache.');
  102. $source->cacheMetadata(true);
  103. }
  104. return $source->schemaCollection();
  105. }
  106. /**
  107. * Get the option parser for this shell.
  108. *
  109. * @return \Cake\Console\ConsoleOptionParser
  110. */
  111. public function getOptionParser()
  112. {
  113. $parser = parent::getOptionParser();
  114. $parser->addSubcommand('clear', [
  115. 'help' => 'Clear all metadata caches for the connection. If a ' .
  116. 'table name is provided, only that table will be removed.',
  117. ])->addSubcommand('build', [
  118. 'help' => 'Build all metadata caches for the connection. If a ' .
  119. 'table name is provided, only that table will be cached.',
  120. ])->addOption('connection', [
  121. 'help' => 'The connection to build/clear metadata cache data for.',
  122. 'short' => 'c',
  123. 'default' => 'default',
  124. ])->addArgument('name', [
  125. 'help' => 'A specific table you want to clear/refresh cached data for.',
  126. 'optional' => true,
  127. ]);
  128. return $parser;
  129. }
  130. }