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