CacheRegistry.php 3.4 KB

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