CachedCollection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Schema;
  17. use Cake\Cache\Cache;
  18. use Cake\Datasource\ConnectionInterface;
  19. /**
  20. * Extends the schema collection class to provide caching
  21. */
  22. class CachedCollection extends Collection
  23. {
  24. /**
  25. * The name of the cache config key to use for caching table metadata,
  26. * of false if disabled.
  27. *
  28. * @var string|bool
  29. */
  30. protected $_cache = false;
  31. /**
  32. * Constructor.
  33. *
  34. * @param \Cake\Datasource\ConnectionInterface $connection The connection instance.
  35. * @param string|bool $cacheKey The cache key or boolean false to disable caching.
  36. */
  37. public function __construct(ConnectionInterface $connection, $cacheKey = true)
  38. {
  39. parent::__construct($connection);
  40. $this->setCacheMetadata($cacheKey);
  41. }
  42. /**
  43. * {@inheritDoc}
  44. *
  45. */
  46. public function describe(string $name, array $options = []): TableSchemaInterface
  47. {
  48. $options += ['forceRefresh' => false];
  49. $cacheConfig = $this->getCacheMetadata();
  50. $cacheKey = $this->cacheKey($name);
  51. if (!empty($cacheConfig) && !$options['forceRefresh']) {
  52. $cached = Cache::read($cacheKey, $cacheConfig);
  53. if ($cached !== null) {
  54. return $cached;
  55. }
  56. }
  57. $table = parent::describe($name, $options);
  58. if (!empty($cacheConfig)) {
  59. Cache::write($cacheKey, $table, $cacheConfig);
  60. }
  61. return $table;
  62. }
  63. /**
  64. * Get the cache key for a given name.
  65. *
  66. * @param string $name The name to get a cache key for.
  67. * @return string The cache key.
  68. */
  69. public function cacheKey(string $name): string
  70. {
  71. return $this->_connection->configName() . '_' . $name;
  72. }
  73. /**
  74. * Sets the cache config name to use for caching table metadata, or
  75. * disables it if false is passed.
  76. *
  77. * @param string|bool $enable Whether or not to enable caching
  78. * @return $this
  79. */
  80. public function setCacheMetadata($enable)
  81. {
  82. if ($enable === true) {
  83. $enable = '_cake_model_';
  84. }
  85. $this->_cache = $enable;
  86. return $this;
  87. }
  88. /**
  89. * Gets the cache config name to use for caching table metadata, false means disabled.
  90. *
  91. * @return string|bool
  92. */
  93. public function getCacheMetadata()
  94. {
  95. return $this->_cache;
  96. }
  97. }