SchemaCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.6.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database;
  16. use Cake\Cache\Cache;
  17. use Cake\Database\Connection;
  18. use RuntimeException;
  19. /**
  20. * Schema Cache.
  21. *
  22. * This tool is intended to be used by deployment scripts so that you
  23. * can prevent thundering herd effects on the metadata cache when new
  24. * versions of your application are deployed, or when migrations
  25. * requiring updated metadata are required.
  26. */
  27. class SchemaCache
  28. {
  29. /**
  30. * Schema
  31. *
  32. * @var \Cake\Database\Schema\CachedCollection
  33. */
  34. protected $_schema;
  35. /**
  36. * Constructor
  37. *
  38. * @param string|\Cake\Datasource\ConnectionInterface $connection Connection name to get the schema for or a connection instance
  39. */
  40. public function __construct($connection)
  41. {
  42. $this->_schema = $this->getSchema($connection);
  43. }
  44. /**
  45. * Build metadata.
  46. *
  47. * @param string|null $name The name of the table to build cache data for.
  48. * @return array Returns a list build table caches
  49. */
  50. public function build($name = null)
  51. {
  52. $tables = [$name];
  53. if (empty($name)) {
  54. $tables = $this->_schema->listTables();
  55. }
  56. foreach ($tables as $table) {
  57. $this->_schema->describe($table, ['forceRefresh' => true]);
  58. }
  59. return $tables;
  60. }
  61. /**
  62. * Clear metadata.
  63. *
  64. * @param string|null $name The name of the table to clear cache data for.
  65. * @return array Returns a list of cleared table caches
  66. */
  67. public function clear($name = null)
  68. {
  69. $tables = [$name];
  70. if (empty($name)) {
  71. $tables = $this->_schema->listTables();
  72. }
  73. $configName = $this->_schema->getCacheMetadata();
  74. foreach ($tables as $table) {
  75. $key = $this->_schema->cacheKey($table);
  76. Cache::delete($key, $configName);
  77. }
  78. return $tables;
  79. }
  80. /**
  81. * Helper method to get the schema collection.
  82. *
  83. * @param \Cake\Database\Connection $connection Connection object
  84. * @return \Cake\Database\Schema\Collection|\Cake\Database\Schema\CachedCollection
  85. * @throws \RuntimeException If given connection object is not compatible with schema caching
  86. */
  87. public function getSchema(Connection $connection)
  88. {
  89. if (!method_exists($connection, 'schemaCollection')) {
  90. throw new RuntimeException('The given connection object is not compatible with schema caching, as it does not implement a "schemaCollection()" method.');
  91. }
  92. $config = $connection->config();
  93. if (empty($config['cacheMetadata'])) {
  94. $connection->cacheMetadata(true);
  95. }
  96. return $connection->getSchemaCollection();
  97. }
  98. }