ChainedFastBackendFactory.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. use Drupal\Core\Site\Settings;
  4. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  5. /**
  6. * Defines the chained fast cache backend factory.
  7. *
  8. * @see \Drupal\Core\Cache\ChainedFastBackend
  9. */
  10. class ChainedFastBackendFactory implements CacheFactoryInterface {
  11. use ContainerAwareTrait;
  12. /**
  13. * The service name of the consistent backend factory.
  14. *
  15. * @var string
  16. */
  17. protected $consistentServiceName;
  18. /**
  19. * The service name of the fast backend factory.
  20. *
  21. * @var string
  22. */
  23. protected $fastServiceName;
  24. /**
  25. * Constructs ChainedFastBackendFactory object.
  26. *
  27. * @param \Drupal\Core\Site\Settings|null $settings
  28. * (optional) The settings object.
  29. * @param string|null $consistent_service_name
  30. * (optional) The service name of the consistent backend factory. Defaults
  31. * to:
  32. * - $settings->get('cache')['default'] (if specified)
  33. * - 'cache.backend.database' (if the above isn't specified)
  34. * @param string|null $fast_service_name
  35. * (optional) The service name of the fast backend factory. Defaults to:
  36. * - 'cache.backend.apcu' (if the PHP process has APCu enabled)
  37. * - NULL (if the PHP process doesn't have APCu enabled)
  38. */
  39. public function __construct(Settings $settings = NULL, $consistent_service_name = NULL, $fast_service_name = NULL) {
  40. // Default the consistent backend to the site's default backend.
  41. if (!isset($consistent_service_name)) {
  42. $cache_settings = isset($settings) ? $settings->get('cache') : [];
  43. $consistent_service_name = isset($cache_settings['default']) ? $cache_settings['default'] : 'cache.backend.database';
  44. }
  45. // Default the fast backend to APCu if it's available.
  46. if (!isset($fast_service_name) && function_exists('apcu_fetch')) {
  47. $fast_service_name = 'cache.backend.apcu';
  48. }
  49. $this->consistentServiceName = $consistent_service_name;
  50. // Do not use the fast chained backend during installation. In those cases,
  51. // we expect many cache invalidations and writes, the fast chained cache
  52. // backend performs badly in such a scenario.
  53. if (!drupal_installation_attempted()) {
  54. $this->fastServiceName = $fast_service_name;
  55. }
  56. }
  57. /**
  58. * Instantiates a chained, fast cache backend class for a given cache bin.
  59. *
  60. * @param string $bin
  61. * The cache bin for which a cache backend object should be returned.
  62. *
  63. * @return \Drupal\Core\Cache\CacheBackendInterface
  64. * The cache backend object associated with the specified bin.
  65. */
  66. public function get($bin) {
  67. // Use the chained backend only if there is a fast backend available;
  68. // otherwise, just return the consistent backend directly.
  69. if (isset($this->fastServiceName)) {
  70. return new ChainedFastBackend(
  71. $this->container->get($this->consistentServiceName)->get($bin),
  72. $this->container->get($this->fastServiceName)->get($bin),
  73. $bin
  74. );
  75. }
  76. else {
  77. return $this->container->get($this->consistentServiceName)->get($bin);
  78. }
  79. }
  80. }