Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Cache / ChainedFastBackendFactory.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4 use Drupal\Core\Site\Settings;
5 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
6
7 /**
8  * Defines the chained fast cache backend factory.
9  *
10  * @see \Drupal\Core\Cache\ChainedFastBackend
11  */
12 class ChainedFastBackendFactory implements CacheFactoryInterface {
13
14   use ContainerAwareTrait;
15
16   /**
17    * The service name of the consistent backend factory.
18    *
19    * @var string
20    */
21   protected $consistentServiceName;
22
23   /**
24    * The service name of the fast backend factory.
25    *
26    * @var string
27    */
28   protected $fastServiceName;
29
30   /**
31    * Constructs ChainedFastBackendFactory object.
32    *
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
37    *   to:
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)
44    */
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';
50     }
51
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';
55     }
56
57     $this->consistentServiceName = $consistent_service_name;
58
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;
64     }
65   }
66
67   /**
68    * Instantiates a chained, fast cache backend class for a given cache bin.
69    *
70    * @param string $bin
71    *   The cache bin for which a cache backend object should be returned.
72    *
73    * @return \Drupal\Core\Cache\CacheBackendInterface
74    *   The cache backend object associated with the specified bin.
75    */
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),
83         $bin
84       );
85     }
86     else {
87       return $this->container->get($this->consistentServiceName)->get($bin);
88     }
89   }
90
91 }