ChainedFastBackendFactory.php 3.1 KB

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