3 namespace Drupal\Core\Cache;
5 use Drupal\Core\Site\Settings;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
9 * Defines the chained fast cache backend factory.
11 * @see \Drupal\Core\Cache\ChainedFastBackend
13 class ChainedFastBackendFactory implements CacheFactoryInterface {
15 use ContainerAwareTrait;
18 * The service name of the consistent backend factory.
22 protected $consistentServiceName;
25 * The service name of the fast backend factory.
29 protected $fastServiceName;
32 * Constructs ChainedFastBackendFactory object.
34 * @param \Drupal\Core\Site\Settings|null $settings
35 * (optional) The settings object.
36 * @param string|null $consistent_service_name
37 * (optional) The service name of the consistent backend factory. Defaults
39 * - $settings->get('cache')['default'] (if specified)
40 * - 'cache.backend.database' (if the above isn't specified)
41 * @param string|null $fast_service_name
42 * (optional) The service name of the fast backend factory. Defaults to:
43 * - 'cache.backend.apcu' (if the PHP process has APCu enabled)
44 * - NULL (if the PHP process doesn't have APCu enabled)
46 public function __construct(Settings $settings = NULL, $consistent_service_name = NULL, $fast_service_name = NULL) {
47 // Default the consistent backend to the site's default backend.
48 if (!isset($consistent_service_name)) {
49 $cache_settings = isset($settings) ? $settings->get('cache') : [];
50 $consistent_service_name = isset($cache_settings['default']) ? $cache_settings['default'] : 'cache.backend.database';
53 // Default the fast backend to APCu if it's available.
54 if (!isset($fast_service_name) && function_exists('apcu_fetch')) {
55 $fast_service_name = 'cache.backend.apcu';
58 $this->consistentServiceName = $consistent_service_name;
60 // Do not use the fast chained backend during installation. In those cases,
61 // we expect many cache invalidations and writes, the fast chained cache
62 // backend performs badly in such a scenario.
63 if (!drupal_installation_attempted()) {
64 $this->fastServiceName = $fast_service_name;
69 * Instantiates a chained, fast cache backend class for a given cache bin.
72 * The cache bin for which a cache backend object should be returned.
74 * @return \Drupal\Core\Cache\CacheBackendInterface
75 * The cache backend object associated with the specified bin.
77 public function get($bin) {
78 // Use the chained backend only if there is a fast backend available;
79 // otherwise, just return the consistent backend directly.
80 if (isset($this->fastServiceName)) {
81 return new ChainedFastBackend(
82 $this->container->get($this->consistentServiceName)->get($bin),
83 $this->container->get($this->fastServiceName)->get($bin),
88 return $this->container->get($this->consistentServiceName)->get($bin);