function DatabaseBackend::garbageCollection

Same name in this branch
  1. 9 core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend::garbageCollection()
Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend::garbageCollection()
  2. 8.9.x core/lib/Drupal/Core/Cache/DatabaseBackend.php \Drupal\Core\Cache\DatabaseBackend::garbageCollection()
  3. 10 core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend::garbageCollection()
  4. 10 core/lib/Drupal/Core/Cache/DatabaseBackend.php \Drupal\Core\Cache\DatabaseBackend::garbageCollection()
  5. 11.x core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend::garbageCollection()
  6. 11.x core/lib/Drupal/Core/Cache/DatabaseBackend.php \Drupal\Core\Cache\DatabaseBackend::garbageCollection()

Overrides CacheBackendInterface::garbageCollection

File

core/lib/Drupal/Core/Cache/DatabaseBackend.php, line 366

Class

DatabaseBackend
Defines a default cache implementation.

Namespace

Drupal\Core\Cache

Code

public function garbageCollection() {
    try {
        // Bounded size cache bin, using FIFO.
        if ($this->maxRows !== static::MAXIMUM_NONE) {
            $first_invalid_create_time = $this->connection
                ->select($this->bin)
                ->fields($this->bin, [
                'created',
            ])
                ->orderBy("{$this->bin}.created", 'DESC')
                ->range($this->maxRows, 1)
                ->execute()
                ->fetchField();
            if ($first_invalid_create_time) {
                $this->connection
                    ->delete($this->bin)
                    ->condition('created', $first_invalid_create_time, '<=')
                    ->execute();
            }
        }
        $this->connection
            ->delete($this->bin)
            ->condition('expire', Cache::PERMANENT, '<>')
            ->condition('expire', REQUEST_TIME, '<')
            ->execute();
    } catch (\Exception $e) {
        // If the table does not exist, it surely does not have garbage in it.
        // If the table exists, the next garbage collection will clean up.
        // There is nothing to do.
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.