OrmCacheShell.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Console\Command;
  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. * Build metadata.
  31. *
  32. * @param $name string
  33. * @return boolean
  34. */
  35. public function build($name = null) {
  36. $schema = $this->_getSchema();
  37. if (!$schema) {
  38. return false;
  39. }
  40. if (!$schema->cacheMetadata()) {
  41. $this->_io->verbose('Metadata cache was disabled in config. Enabling to write cache.');
  42. $schema->cacheMetadata(true);
  43. }
  44. $tables = [$name];
  45. if (empty($name)) {
  46. $tables = $schema->listTables();
  47. }
  48. foreach ($tables as $table) {
  49. $this->_io->verbose('Building metadata cache for ' . $table);
  50. $schema->describe($table, ['forceRefresh' => true]);
  51. }
  52. $this->out('<success>Cache build complete</success>');
  53. return true;
  54. }
  55. /**
  56. * Clear metadata.
  57. *
  58. * @param $name string
  59. * @return boolean
  60. */
  61. public function clear($name = null) {
  62. $schema = $this->_getSchema();
  63. if (!$schema) {
  64. return false;
  65. }
  66. $tables = [$name];
  67. if (empty($name)) {
  68. $tables = $schema->listTables();
  69. }
  70. if (!$schema->cacheMetadata()) {
  71. $this->_io->verbose('Metadata cache was disabled in config. Enabling to clear cache.');
  72. $schema->cacheMetadata(true);
  73. }
  74. $configName = $schema->cacheMetadata();
  75. foreach ($tables as $table) {
  76. $this->_io->verbose(sprintf(
  77. 'Clearing metadata cache from "%s" for %s',
  78. $configName,
  79. $table
  80. ));
  81. $key = $schema->cacheKey($table);
  82. Cache::delete($key, $configName);
  83. }
  84. $this->out('<success>Cache clear complete</success>');
  85. return true;
  86. }
  87. /**
  88. * Helper method to get the schema collection.
  89. *
  90. * @return false|\Cake\Database\Schema\Collection
  91. */
  92. protected function _getSchema() {
  93. $source = ConnectionManager::get($this->params['connection']);
  94. if (!method_exists($source, 'schemaCollection')) {
  95. $msg = sprintf('The "%s" connection is not compatible with orm caching, ' .
  96. 'as it does not implement a "schemaCollection()" method.',
  97. $this->params['connection']);
  98. $this->error($msg);
  99. return false;
  100. }
  101. return $source->schemaCollection();
  102. }
  103. /**
  104. * Get the option parser for this shell.
  105. *
  106. * @return \Cake\Console\ConsoleOptionParser
  107. */
  108. public function getOptionParser() {
  109. $parser = parent::getOptionParser();
  110. $parser->addSubcommand('clear', [
  111. 'help' => 'Clear all metadata caches for the connection. If a ' .
  112. 'table name is provided, only that table will be removed.',
  113. ])->addSubcommand('build', [
  114. 'help' => 'Build all metadata caches for the connection. If a ' .
  115. 'table name is provided, only that table will be cached.',
  116. ])->addOption('connection', [
  117. 'help' => 'The connection to build/clear metadata cache data for.',
  118. 'short' => 'c',
  119. 'default' => 'default',
  120. ])->addArgument('name', [
  121. 'help' => 'A specific table you want to clear/refresh cached data for.',
  122. 'optional' => true,
  123. ]);
  124. return $parser;
  125. }
  126. }