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