DatabaseBackendFactory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. use Drupal\Core\Database\Connection;
  4. use Drupal\Core\Site\Settings;
  5. class DatabaseBackendFactory implements CacheFactoryInterface {
  6. /**
  7. * The database connection.
  8. *
  9. * @var \Drupal\Core\Database\Connection
  10. */
  11. protected $connection;
  12. /**
  13. * The cache tags checksum provider.
  14. *
  15. * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
  16. */
  17. protected $checksumProvider;
  18. /**
  19. * The settings array.
  20. *
  21. * @var \Drupal\Core\Site\Settings
  22. */
  23. protected $settings;
  24. /**
  25. * Constructs the DatabaseBackendFactory object.
  26. *
  27. * @param \Drupal\Core\Database\Connection $connection
  28. * Database connection
  29. * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
  30. * The cache tags checksum provider.
  31. * @param \Drupal\Core\Site\Settings $settings
  32. * (optional) The settings array.
  33. *
  34. * @throws \BadMethodCallException
  35. */
  36. public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, Settings $settings = NULL) {
  37. $this->connection = $connection;
  38. $this->checksumProvider = $checksum_provider;
  39. $this->settings = $settings ?: Settings::getInstance();
  40. }
  41. /**
  42. * Gets DatabaseBackend for the specified cache bin.
  43. *
  44. * @param $bin
  45. * The cache bin for which the object is created.
  46. *
  47. * @return \Drupal\Core\Cache\DatabaseBackend
  48. * The cache backend object for the specified cache bin.
  49. */
  50. public function get($bin) {
  51. $max_rows = $this->getMaxRowsForBin($bin);
  52. return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $max_rows);
  53. }
  54. /**
  55. * Gets the max rows for the specified cache bin.
  56. *
  57. * @param string $bin
  58. * The cache bin for which the object is created.
  59. *
  60. * @return int
  61. * The maximum number of rows for the given bin. Defaults to
  62. * DatabaseBackend::DEFAULT_MAX_ROWS.
  63. */
  64. protected function getMaxRowsForBin($bin) {
  65. $max_rows_settings = $this->settings->get('database_cache_max_rows');
  66. // First, look for a cache bin specific setting.
  67. if (isset($max_rows_settings['bins'][$bin])) {
  68. $max_rows = $max_rows_settings['bins'][$bin];
  69. }
  70. // Second, use configured default backend.
  71. elseif (isset($max_rows_settings['default'])) {
  72. $max_rows = $max_rows_settings['default'];
  73. }
  74. else {
  75. // Fall back to the default max rows if nothing else is configured.
  76. $max_rows = DatabaseBackend::DEFAULT_MAX_ROWS;
  77. }
  78. return $max_rows;
  79. }
  80. }