You are here

public function Cache::clear in MongoDB 7

File

mongodb_cache/mongodb_cache_plugin.php, line 362

Class

Cache
MongoDB cache implementation.

Namespace

Drupal\mongodb_cache

Code

public function clear($cid = NULL, $wildcard = FALSE) {
  $minimum_lifetime = variable_get('cache_lifetime', 0);
  if (empty($cid)) {
    if ($minimum_lifetime) {

      // We store the time in the current user's $user->cache variable which
      // will be saved into the sessions bin by _drupal_session_write(). We
      // then simulate that the cache was flushed for this user by not
      // returning cached data that was cached before the timestamp.
      $_SESSION['cache_expiration'][$this->bin] = REQUEST_TIME;
      $flush_timestamp = $this
        ->getFlushTimestamp();
      if (empty($flush_timestamp)) {

        // This is the first request to clear the cache, start a timer.
        $this
          ->setFlushTimestamp(REQUEST_TIME);
      }
      elseif (REQUEST_TIME > $flush_timestamp + $minimum_lifetime) {

        // Clear the cache for everyone, cache_lifetime seconds have passed
        // since the first request to clear the cache.
        $criteria = [
          'expire' => [
            '$ne' => CACHE_PERMANENT,
            '$lte' => REQUEST_TIME,
          ],
        ];
        $this
          ->attemptRemove($criteria);
        $this
          ->setFlushTimestamp(REQUEST_TIME + $this->stampedeDelay);
      }
    }
    else {

      // No minimum cache lifetime, flush all temporary cache entries now.
      $criteria = [
        'expire' => [
          '$ne' => CACHE_PERMANENT,
          '$lte' => REQUEST_TIME,
        ],
      ];
      $this
        ->attemptRemove($criteria);
    }
  }
  else {
    if ($wildcard) {
      if ($cid == '*') {
        $criteria = [];
        $this
          ->attemptRemove($criteria);
      }
      else {
        $criteria = [
          'cid' => new \MongoRegex('/' . preg_quote($cid) . '.*/'),
        ];
        $this
          ->attemptRemove($criteria);
      }
    }
    elseif (is_array($cid)) {

      // Delete in chunks in case a large array is passed.
      do {
        $criteria = [
          'cid' => [
            '$in' => array_map('strval', array_splice($cid, 0, 1000)),
          ],
        ];
        $this
          ->attemptRemove($criteria);
      } while (count($cid));
    }
    else {
      $criteria = [
        '_id' => (string) $cid,
      ];
      $this
        ->attemptRemove($criteria);
    }
  }
}