3 namespace Drupal\Core\Cache;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Site\Settings;
8 class DatabaseBackendFactory implements CacheFactoryInterface {
11 * The database connection.
13 * @var \Drupal\Core\Database\Connection
15 protected $connection;
18 * The cache tags checksum provider.
20 * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
22 protected $checksumProvider;
27 * @var \Drupal\Core\Site\Settings
32 * Constructs the DatabaseBackendFactory object.
34 * @param \Drupal\Core\Database\Connection $connection
36 * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
37 * The cache tags checksum provider.
38 * @param \Drupal\Core\Site\Settings $settings
39 * (optional) The settings array.
41 * @throws \BadMethodCallException
43 public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, Settings $settings = NULL) {
44 $this->connection = $connection;
45 $this->checksumProvider = $checksum_provider;
46 $this->settings = $settings ?: Settings::getInstance();
50 * Gets DatabaseBackend for the specified cache bin.
53 * The cache bin for which the object is created.
55 * @return \Drupal\Core\Cache\DatabaseBackend
56 * The cache backend object for the specified cache bin.
58 public function get($bin) {
59 $max_rows = $this->getMaxRowsForBin($bin);
60 return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $max_rows);
64 * Gets the max rows for the specified cache bin.
67 * The cache bin for which the object is created.
70 * The maximum number of rows for the given bin. Defaults to
71 * DatabaseBackend::DEFAULT_MAX_ROWS.
73 protected function getMaxRowsForBin($bin) {
74 $max_rows_settings = $this->settings->get('database_cache_max_rows');
75 // First, look for a cache bin specific setting.
76 if (isset($max_rows_settings['bins'][$bin])) {
77 $max_rows = $max_rows_settings['bins'][$bin];
79 // Second, use configured default backend.
80 elseif (isset($max_rows_settings['default'])) {
81 $max_rows = $max_rows_settings['default'];
84 // Fall back to the default max rows if nothing else is configured.
85 $max_rows = DatabaseBackend::DEFAULT_MAX_ROWS;