6bde00babd745f2d08ccc1ba0fa744de5d4b1058
[yaffs-website] / web / core / lib / Drupal / Core / Cache / DatabaseBackendFactory.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Site\Settings;
7
8 class DatabaseBackendFactory implements CacheFactoryInterface {
9
10   /**
11    * The database connection.
12    *
13    * @var \Drupal\Core\Database\Connection
14    */
15   protected $connection;
16
17   /**
18    * The cache tags checksum provider.
19    *
20    * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
21    */
22   protected $checksumProvider;
23
24   /**
25    * The settings array.
26    *
27    * @var \Drupal\Core\Site\Settings
28    */
29   protected $settings;
30
31   /**
32    * Constructs the DatabaseBackendFactory object.
33    *
34    * @param \Drupal\Core\Database\Connection $connection
35    *   Database 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.
40    *
41    * @throws \BadMethodCallException
42    */
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();
47   }
48
49   /**
50    * Gets DatabaseBackend for the specified cache bin.
51    *
52    * @param $bin
53    *   The cache bin for which the object is created.
54    *
55    * @return \Drupal\Core\Cache\DatabaseBackend
56    *   The cache backend object for the specified cache bin.
57    */
58   public function get($bin) {
59     $max_rows = $this->getMaxRowsForBin($bin);
60     return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $max_rows);
61   }
62
63   /**
64    * Gets the max rows for the specified cache bin.
65    *
66    * @param string $bin
67    *   The cache bin for which the object is created.
68    *
69    * @return int
70    *   The maximum number of rows for the given bin. Defaults to
71    *   DatabaseBackend::DEFAULT_MAX_ROWS.
72    */
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];
78     }
79     // Second, use configured default backend.
80     elseif (isset($max_rows_settings['default'])) {
81       $max_rows = $max_rows_settings['default'];
82     }
83     else {
84       // Fall back to the default max rows if nothing else is configured.
85       $max_rows = DatabaseBackend::DEFAULT_MAX_ROWS;
86     }
87     return $max_rows;
88   }
89
90 }