CacheFactory.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. /**
  4. * Defines the cache backend factory.
  5. */
  6. use Drupal\Core\Site\Settings;
  7. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  8. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  9. class CacheFactory implements CacheFactoryInterface, ContainerAwareInterface {
  10. use ContainerAwareTrait;
  11. /**
  12. * The settings array.
  13. *
  14. * @var \Drupal\Core\Site\Settings
  15. */
  16. protected $settings;
  17. /**
  18. * A map of cache bin to default cache backend service name.
  19. *
  20. * All mappings in $settings takes precedence over this, but this can be used
  21. * to optimize cache storage for a Drupal installation without cache
  22. * customizations in settings.php. For example, this can be used to map the
  23. * 'bootstrap' bin to 'cache.backend.chainedfast', while allowing other bins
  24. * to fall back to the global default of 'cache.backend.database'.
  25. *
  26. * @var array
  27. */
  28. protected $defaultBinBackends;
  29. /**
  30. * Constructs CacheFactory object.
  31. *
  32. * @param \Drupal\Core\Site\Settings $settings
  33. * The settings array.
  34. * @param array $default_bin_backends
  35. * (optional) A mapping of bin to backend service name. Mappings in
  36. * $settings take precedence over this.
  37. */
  38. public function __construct(Settings $settings, array $default_bin_backends = []) {
  39. $this->settings = $settings;
  40. $this->defaultBinBackends = $default_bin_backends;
  41. }
  42. /**
  43. * Instantiates a cache backend class for a given cache bin.
  44. *
  45. * By default, this returns an instance of the
  46. * Drupal\Core\Cache\DatabaseBackend class.
  47. *
  48. * Classes implementing Drupal\Core\Cache\CacheBackendInterface can register
  49. * themselves both as a default implementation and for specific bins.
  50. *
  51. * @param string $bin
  52. * The cache bin for which a cache backend object should be returned.
  53. *
  54. * @return \Drupal\Core\Cache\CacheBackendInterface
  55. * The cache backend object associated with the specified bin.
  56. */
  57. public function get($bin) {
  58. $cache_settings = $this->settings->get('cache');
  59. // First, look for a cache bin specific setting.
  60. if (isset($cache_settings['bins'][$bin])) {
  61. $service_name = $cache_settings['bins'][$bin];
  62. }
  63. // Second, use the default backend specified by the cache bin.
  64. elseif (isset($this->defaultBinBackends[$bin])) {
  65. $service_name = $this->defaultBinBackends[$bin];
  66. }
  67. // Third, use configured default backend.
  68. elseif (isset($cache_settings['default'])) {
  69. $service_name = $cache_settings['default'];
  70. }
  71. else {
  72. // Fall back to the database backend if nothing else is configured.
  73. $service_name = 'cache.backend.database';
  74. }
  75. return $this->container->get($service_name)->get($bin);
  76. }
  77. }