You are here

protected function Cache::garbageCollection in MongoDB 7

Garbage collection for get() and getMultiple().

2 calls to Cache::garbageCollection()
Cache::get in mongodb_cache/mongodb_cache_plugin.php
Cache::getMultiple in mongodb_cache/mongodb_cache_plugin.php

File

mongodb_cache/mongodb_cache_plugin.php, line 241

Class

Cache
MongoDB cache implementation.

Namespace

Drupal\mongodb_cache

Code

protected function garbageCollection() {

  // Garbage collection only required when enforcing a minimum cache lifetime.
  $flush_timestamp = $this
    ->getFlushTimestamp();
  if ($flush_timestamp && $flush_timestamp + variable_get('cache_lifetime', 0) <= REQUEST_TIME) {

    // Reset the variable immediately to prevent a meltdown under heavy load.
    $this
      ->setFlushTimestamp(0);

    // Remove non-permanently cached items from the collection.
    $criteria = [
      'expire' => [
        '$lte' => $flush_timestamp,
        '$ne' => CACHE_PERMANENT,
      ],
    ];
    try {
      $this->collection
        ->remove($criteria, $this->unsafe);
    } catch (\MongoException $e) {
      self::notifyException($e);
    }

    // Re-enable the expiration mechanism.
    $this
      ->setFlushTimestamp(REQUEST_TIME + $this->stampedeDelay);
  }
}