CachedCollection.php 3.6 KB

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