CacheRegistry.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Cache;
  16. use BadMethodCallException;
  17. use Cake\Core\App;
  18. use Cake\Core\ObjectRegistry;
  19. use RuntimeException;
  20. /**
  21. * An object registry for cache engines.
  22. *
  23. * Used by Cake\Cache\Cache to load and manage cache engines.
  24. *
  25. * @extends \Cake\Core\ObjectRegistry<\Cake\Cache\CacheEngine>
  26. */
  27. class CacheRegistry extends ObjectRegistry
  28. {
  29. /**
  30. * Resolve a cache engine classname.
  31. *
  32. * Part of the template method for Cake\Core\ObjectRegistry::load()
  33. *
  34. * @param string $class Partial classname to resolve.
  35. * @return string|false Either the correct classname or false.
  36. */
  37. protected function _resolveClassName($class)
  38. {
  39. if (is_object($class)) {
  40. return $class;
  41. }
  42. return App::className($class, 'Cache/Engine', 'Engine');
  43. }
  44. /**
  45. * Throws an exception when a cache engine is missing.
  46. *
  47. * Part of the template method for Cake\Core\ObjectRegistry::load()
  48. *
  49. * @param string $class The classname that is missing.
  50. * @param string|null $plugin The plugin the cache is missing in.
  51. * @return void
  52. * @throws \BadMethodCallException
  53. */
  54. protected function _throwMissingClassError($class, $plugin)
  55. {
  56. throw new BadMethodCallException(sprintf('Cache engine %s is not available.', $class));
  57. }
  58. /**
  59. * Create the cache engine instance.
  60. *
  61. * Part of the template method for Cake\Core\ObjectRegistry::load()
  62. *
  63. * @param string|\Cake\Cache\CacheEngine $class The classname or object to make.
  64. * @param string $alias The alias of the object.
  65. * @param array $config An array of settings to use for the cache engine.
  66. * @return \Cake\Cache\CacheEngine The constructed CacheEngine class.
  67. * @throws \RuntimeException when an object doesn't implement the correct interface.
  68. */
  69. protected function _create($class, $alias, $config)
  70. {
  71. if (is_object($class)) {
  72. $instance = $class;
  73. }
  74. unset($config['className']);
  75. if (!isset($instance)) {
  76. $instance = new $class($config);
  77. }
  78. if (!($instance instanceof CacheEngine)) {
  79. throw new RuntimeException(
  80. 'Cache engines must use Cake\Cache\CacheEngine as a base class.'
  81. );
  82. }
  83. if (!$instance->init($config)) {
  84. throw new RuntimeException(
  85. sprintf('Cache engine %s is not properly configured.', get_class($instance))
  86. );
  87. }
  88. $config = $instance->getConfig();
  89. if (!empty($config['probability']) && time() % $config['probability'] === 0) {
  90. $instance->gc();
  91. }
  92. return $instance;
  93. }
  94. /**
  95. * Remove a single adapter from the registry.
  96. *
  97. * @param string $name The adapter name.
  98. * @return $this
  99. */
  100. public function unload($name)
  101. {
  102. unset($this->_loaded[$name]);
  103. return $this;
  104. }
  105. }