CacheRegistry.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Cache;
  15. use Cake\Core\App;
  16. use Cake\Error;
  17. use Cake\Utility\ObjectRegistry;
  18. /**
  19. * An object registry for cache engines.
  20. *
  21. * Used by Cake\Cache\Cache to load and manage cache engines.
  22. *
  23. * @since CakePHP 3.0
  24. */
  25. class CacheRegistry extends ObjectRegistry {
  26. /**
  27. * Resolve a cache engine classname.
  28. *
  29. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  30. *
  31. * @param string $class Partial classname to resolve.
  32. * @return string|false Either the correct classname or false.
  33. */
  34. protected function _resolveClassName($class) {
  35. if (is_object($class)) {
  36. return $class;
  37. }
  38. return App::classname($class, 'Cache/Engine', 'Engine');
  39. }
  40. /**
  41. * Throws an exception when a cache engine is missing.
  42. *
  43. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  44. *
  45. * @param string $class The classname that is missing.
  46. * @param string $plugin The plugin the cache is missing in.
  47. * @throws Cake\Error\Exception
  48. */
  49. protected function _throwMissingClassError($class, $plugin) {
  50. throw new Error\Exception(__d('cake_dev', 'Cache engine %s is not available.', $class));
  51. }
  52. /**
  53. * Create the cache engine instance.
  54. *
  55. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  56. * @param string|CacheEngine $class The classname or object to make.
  57. * @param array $settings An array of settings to use for the cache engine.
  58. * @return CacheEngine The constructed CacheEngine class.
  59. * @throws Cake\Error\Exception when an object doesn't implement
  60. * the correct interface.
  61. */
  62. protected function _create($class, $settings) {
  63. if (is_object($class)) {
  64. $instance = $class;
  65. }
  66. unset($settings['className']);
  67. if (!isset($instance)) {
  68. $instance = new $class($settings);
  69. }
  70. if (!($instance instanceof CacheEngine)) {
  71. throw new Error\Exception(__d(
  72. 'cake_dev',
  73. 'Cache engines must use Cake\Cache\CacheEngine as a base class.'
  74. ));
  75. }
  76. if (!$instance->init($settings)) {
  77. throw new Error\Exception(
  78. __d('cake_dev', 'Cache engine %s is not properly configured.', $class)
  79. );
  80. }
  81. if ($instance->settings['probability'] && time() % $instance->settings['probability'] === 0) {
  82. $instance->gc();
  83. }
  84. return $instance;
  85. }
  86. /**
  87. * Remove a single adapter from the registry.
  88. *
  89. * @param string $name The adapter name.
  90. * @return void
  91. */
  92. public function unload($name) {
  93. unset($this->_loaded[$name]);
  94. }
  95. }