Skip to content
Snippets Groups Projects
Commit 2d4afa9c authored by catch's avatar catch
Browse files

Issue #1730774 by beejeebus, sun: Untangle Cache\DatabaseBackend from...

Issue #1730774 by beejeebus, sun: Untangle Cache\DatabaseBackend from procedural database.inc functions to make it available in early bootstrap.
parent d7ca8aef
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Cache;
use Drupal\Core\Database\Database;
use Exception;
/**
......@@ -53,14 +54,14 @@ function get($cid) {
*/
function getMultiple(&$cids) {
try {
// When serving cached pages, the overhead of using db_select() was found
// When serving cached pages, the overhead of using ::select() was found
// to add around 30% overhead to the request. Since $this->bin is a
// variable, this means the call to db_query() here uses a concatenated
// variable, this means the call to ::query() here uses a concatenated
// string. This is highly discouraged under any other circumstances, and
// is used here only due to the performance overhead we would incur
// otherwise. When serving an uncached page, the overhead of using
// db_select() is a much smaller proportion of the request.
$result = db_query('SELECT cid, data, created, expire, serialized, tags, checksum FROM {' . db_escape_table($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids));
// ::select() is a much smaller proportion of the request.
$result = Database::getConnection()->query('SELECT cid, data, created, expire, serialized, tags, checksum FROM {' . Database::getConnection()->escapeTable($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids));
$cache = array();
foreach ($result as $item) {
$item = $this->prepareItem($item);
......@@ -135,7 +136,7 @@ function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, arra
}
try {
db_merge($this->bin)
Database::getConnection()->merge($this->bin)
->key(array('cid' => $cid))
->fields($fields)
->execute();
......@@ -149,7 +150,7 @@ function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, arra
* Implements Drupal\Core\Cache\CacheBackendInterface::delete().
*/
function delete($cid) {
db_delete($this->bin)
Database::getConnection()->delete($this->bin)
->condition('cid', $cid)
->execute();
}
......@@ -160,7 +161,7 @@ function delete($cid) {
function deleteMultiple(array $cids) {
// Delete in chunks when a large array is passed.
do {
db_delete($this->bin)
Database::getConnection()->delete($this->bin)
->condition('cid', array_splice($cids, 0, 1000), 'IN')
->execute();
}
......@@ -171,8 +172,8 @@ function deleteMultiple(array $cids) {
* Implements Drupal\Core\Cache\CacheBackendInterface::deletePrefix().
*/
function deletePrefix($prefix) {
db_delete($this->bin)
->condition('cid', db_like($prefix) . '%', 'LIKE')
Database::getConnection()->delete($this->bin)
->condition('cid', Database::getConnection()->escapeLike($prefix) . '%', 'LIKE')
->execute();
}
......@@ -180,14 +181,14 @@ function deletePrefix($prefix) {
* Implements Drupal\Core\Cache\CacheBackendInterface::flush().
*/
function flush() {
db_truncate($this->bin)->execute();
Database::getConnection()->truncate($this->bin)->execute();
}
/**
* Implements Drupal\Core\Cache\CacheBackendInterface::expire().
*/
function expire() {
db_delete($this->bin)
Database::getConnection()->delete($this->bin)
->condition('expire', CacheBackendInterface::CACHE_PERMANENT, '<>')
->condition('expire', REQUEST_TIME, '<')
->execute();
......@@ -250,7 +251,7 @@ protected function flattenTags(array $tags) {
public function invalidateTags(array $tags) {
foreach ($this->flattenTags($tags) as $tag) {
unset(self::$tagCache[$tag]);
db_merge('cache_tags')
Database::getConnection()->merge('cache_tags')
->key(array('tag' => $tag))
->fields(array('invalidations' => 1))
->expression('invalidations', 'invalidations + 1')
......@@ -280,9 +281,14 @@ protected function checksumTags($tags) {
}
}
if ($query_tags) {
if ($db_tags = db_query('SELECT tag, invalidations FROM {cache_tags} WHERE tag IN (:tags)', array(':tags' => $query_tags))->fetchAllKeyed()) {
self::$tagCache = array_merge(self::$tagCache, $db_tags);
$checksum += array_sum($db_tags);
try {
if ($db_tags = Database::getConnection()->query('SELECT tag, invalidations FROM {cache_tags} WHERE tag IN (:tags)', array(':tags' => $query_tags))->fetchAllKeyed()) {
self::$tagCache = array_merge(self::$tagCache, $db_tags);
$checksum += array_sum($db_tags);
}
}
catch (Exception $e) {
// The database may not be available, so we'll ignore cache_set requests.
}
}
return $checksum;
......@@ -293,7 +299,7 @@ protected function checksumTags($tags) {
*/
function isEmpty() {
$this->garbageCollection();
$query = db_select($this->bin);
$query = Database::getConnection()->select($this->bin);
$query->addExpression('1');
$result = $query->range(0, 1)
->execute()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment