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.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Schema;
16:
17: use Cake\Cache\Cache;
18: use Cake\Datasource\ConnectionInterface;
19:
20: /**
21: * Extends the schema collection class to provide caching
22: */
23: class CachedCollection extends Collection
24: {
25: /**
26: * The name of the cache config key to use for caching table metadata,
27: * of false if disabled.
28: *
29: * @var string|bool
30: */
31: protected $_cache = false;
32:
33: /**
34: * Constructor.
35: *
36: * @param \Cake\Datasource\ConnectionInterface $connection The connection instance.
37: * @param string|bool $cacheKey The cache key or boolean false to disable caching.
38: */
39: public function __construct(ConnectionInterface $connection, $cacheKey = true)
40: {
41: parent::__construct($connection);
42: $this->setCacheMetadata($cacheKey);
43: }
44:
45: /**
46: * {@inheritDoc}
47: *
48: */
49: public function describe($name, array $options = [])
50: {
51: $options += ['forceRefresh' => false];
52: $cacheConfig = $this->getCacheMetadata();
53: $cacheKey = $this->cacheKey($name);
54:
55: if (!empty($cacheConfig) && !$options['forceRefresh']) {
56: $cached = Cache::read($cacheKey, $cacheConfig);
57: if ($cached !== false) {
58: return $cached;
59: }
60: }
61:
62: $table = parent::describe($name, $options);
63:
64: if (!empty($cacheConfig)) {
65: Cache::write($cacheKey, $table, $cacheConfig);
66: }
67:
68: return $table;
69: }
70:
71: /**
72: * Get the cache key for a given name.
73: *
74: * @param string $name The name to get a cache key for.
75: * @return string The cache key.
76: */
77: public function cacheKey($name)
78: {
79: return $this->_connection->configName() . '_' . $name;
80: }
81:
82: /**
83: * Sets the cache config name to use for caching table metadata, or
84: * disables it if false is passed.
85: *
86: * @param bool $enable Whether or not to enable caching
87: * @return $this
88: */
89: public function setCacheMetadata($enable)
90: {
91: if ($enable === true) {
92: $enable = '_cake_model_';
93: }
94:
95: $this->_cache = $enable;
96:
97: return $this;
98: }
99:
100: /**
101: * Gets the cache config name to use for caching table metadata, false means disabled.
102: *
103: * @return string|bool
104: */
105: public function getCacheMetadata()
106: {
107: return $this->_cache;
108: }
109:
110: /**
111: * Sets the cache config name to use for caching table metadata, or
112: * disables it if false is passed.
113: * If called with no arguments it returns the current configuration name.
114: *
115: * @deprecated 3.4.0 Use setCacheMetadata()/getCacheMetadata()
116: * @param bool|null $enable Whether or not to enable caching
117: * @return string|bool
118: */
119: public function cacheMetadata($enable = null)
120: {
121: deprecationWarning(
122: 'CachedCollection::cacheMetadata() is deprecated. ' .
123: 'Use CachedCollection::setCacheMetadata()/getCacheMetadata() instead.'
124: );
125: if ($enable !== null) {
126: $this->setCacheMetadata($enable);
127: }
128:
129: return $this->getCacheMetadata();
130: }
131: }
132: