class CacheFactory
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Cache/CacheFactory.php \Drupal\Core\Cache\CacheFactory
- 10 core/lib/Drupal/Core/Cache/CacheFactory.php \Drupal\Core\Cache\CacheFactory
- 9 core/lib/Drupal/Core/Cache/CacheFactory.php \Drupal\Core\Cache\CacheFactory
- 8.9.x core/lib/Drupal/Core/Cache/CacheFactory.php \Drupal\Core\Cache\CacheFactory
Defines the cache backend factory.
Hierarchy
- class \Drupal\Core\Cache\CacheFactory implements \Drupal\Core\Cache\CacheFactoryInterface
Expanded class hierarchy of CacheFactory
2 files declare their use of CacheFactory
- CacheFactoryTest.php in core/
tests/ Drupal/ Tests/ Core/ Cache/ CacheFactoryTest.php - ServiceProviderTest.php in core/
tests/ Drupal/ KernelTests/ Core/ ServiceProvider/ ServiceProviderTest.php
2 string references to 'CacheFactory'
- core.services.yml in core/
core.services.yml - core/core.services.yml
- RendererTestBase::setUpMockVariationCacheFactory in core/
tests/ Drupal/ Tests/ Core/ Render/ RendererTestBase.php - Reinitializes the variation cache factory as a mock object.
1 service uses CacheFactory
File
-
core/
lib/ Drupal/ Core/ Cache/ CacheFactory.php, line 11
Namespace
Drupal\Core\CacheView source
class CacheFactory implements CacheFactoryInterface {
/**
* The service container.
*/
protected ContainerInterface $container;
/**
* Sets the service container.
*/
public function setContainer(ContainerInterface $container) : void {
$this->container = $container;
}
/**
* Constructs CacheFactory object.
*
* @param \Drupal\Core\Site\Settings $settings
* The site settings.
* @param array $defaultBinBackends
* (optional) A mapping of bin to backend service name. Mappings in
* $settings take precedence over this.
* @param array $memoryDefaultBinBackends
* (optional) A mapping of bin to backend service name. Mappings in
* $settings take precedence over this.
*/
public function __construct(protected Settings $settings, protected array $defaultBinBackends = [], protected array $memoryDefaultBinBackends = []) {
}
/**
* Instantiates a cache backend class for a given cache bin.
*
* By default, this returns an instance of the
* Drupal\Core\Cache\DatabaseBackend class.
*
* Classes implementing Drupal\Core\Cache\CacheBackendInterface can register
* themselves both as a default implementation and for specific bins.
*
* @param string $bin
* The cache bin for which a cache backend object should be returned.
*
* @return \Drupal\Core\Cache\CacheBackendInterface
* The cache backend object associated with the specified bin.
*/
public function get($bin) {
$cache_settings = $this->settings
->get('cache');
// First, look for a cache bin specific setting.
if (isset($cache_settings['bins'][$bin])) {
$service_name = $cache_settings['bins'][$bin];
}
elseif (isset($this->defaultBinBackends[$bin])) {
$service_name = $this->defaultBinBackends[$bin];
}
elseif (isset($this->memoryDefaultBinBackends[$bin])) {
$service_name = $this->memoryDefaultBinBackends[$bin];
}
elseif (isset($cache_settings['default'])) {
$service_name = $cache_settings['default'];
}
else {
// Fall back to the database backend if nothing else is configured.
$service_name = 'cache.backend.database';
}
return $this->container
->get($service_name)
->get($bin);
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| CacheFactory::$container | protected | property | The service container. | |
| CacheFactory::get | public | function | Instantiates a cache backend class for a given cache bin. | Overrides CacheFactoryInterface::get |
| CacheFactory::setContainer | public | function | Sets the service container. | |
| CacheFactory::__construct | public | function | Constructs CacheFactory object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.