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