CacheRegistry.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(sprintf('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 string $alias The alias of the object.
  58. * @param array $config An array of settings to use for the cache engine.
  59. * @return CacheEngine The constructed CacheEngine class.
  60. * @throws Cake\Error\Exception when an object doesn't implement
  61. * the correct interface.
  62. */
  63. protected function _create($class, $alias, $config) {
  64. if (is_object($class)) {
  65. $instance = $class;
  66. }
  67. unset($config['className']);
  68. if (!isset($instance)) {
  69. $instance = new $class($config);
  70. }
  71. if (!($instance instanceof CacheEngine)) {
  72. throw new Error\Exception(
  73. 'Cache engines must use Cake\Cache\CacheEngine as a base class.'
  74. );
  75. }
  76. if (!$instance->init($config)) {
  77. throw new Error\Exception(
  78. sprintf('Cache engine %s is not properly configured.', get_class($instance))
  79. );
  80. }
  81. $config = $instance->config();
  82. if ($config['probability'] && time() % $config['probability'] === 0) {
  83. $instance->gc();
  84. }
  85. return $instance;
  86. }
  87. /**
  88. * Remove a single adapter from the registry.
  89. *
  90. * @param string $name The adapter name.
  91. * @return void
  92. */
  93. public function unload($name) {
  94. unset($this->_loaded[$name]);
  95. }
  96. }