memcache.inc 25.2 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Implementation of cache.inc with memcache logic included.
 */

8
require_once dirname(__FILE__) . '/dmemcache.inc';
9

10 11 12 13
/**
 * Defines the period after which wildcard clears are not considered valid.
 */
define('MEMCACHE_WILDCARD_INVALIDATE', 86400 * 28);
14 15
define('MEMCACHE_CONTENT_CLEAR', 'MEMCACHE_CONTENT_CLEAR');

16 17 18
/**
 * Implementation of cache.inc with memcache logic included
 */
19
class MemCacheDrupal implements DrupalCacheInterface {
20 21 22 23 24 25 26 27 28
  protected $memcache;

  /**
   * Constructs a MemCacheDrupal object.
   *
   * @param string $bin
   *   The cache bin for which the object is created.
   */
  public function __construct($bin) {
29 30
    $this->memcache = dmemcache_object($bin);
    $this->bin = $bin;
31

32 33 34
    // If page_cache_without_database is enabled, we have to manually load the
    // $conf array out of cache_bootstrap.
    static $variables_loaded = FALSE;
35 36 37 38
    // NOTE: We don't call drupal_get_bootstrap_phase() because this would
    // break all 7.x Drupal installations prior to 7.33. For more information
    // see https://www.drupal.org/node/667098.
    if (drupal_bootstrap(NULL, FALSE) < DRUPAL_BOOTSTRAP_VARIABLES && !$variables_loaded) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      global $conf;
      $variables_loaded = TRUE;
      // Try loading variables from cache. If that fails, we have to bootstrap
      // further in order to fetch them.
      if ($cached = cache_get('variables', 'cache_bootstrap')) {
        $variables = $cached->data;
        // Make sure variable overrides are applied, see variable_initialize().
        foreach ($conf as $name => $value) {
          $variables[$name] = $value;
        }
        $conf = $variables;
      }
      else {
        drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
      }
    }

56
    $this->reloadVariables();
57
  }
58

59 60 61 62
  /**
   * Implements DrupalCacheInterface::get().
   */
  public function get($cid) {
63 64
    // Clean up.
    $this->garbageCollection();
65 66 67 68
    $cache = dmemcache_get($cid, $this->bin, $this->memcache);
    return $this->valid($cid, $cache) ? $cache : FALSE;
  }

69 70 71 72
  /**
   * Implements DrupalCacheInterface::getMultiple().
   */
  public function getMultiple(&$cids) {
73 74
    // Clean up.
    $this->garbageCollection();
75
    $results = dmemcache_get_multi($cids, $this->bin, $this->memcache);
76
    foreach ($results as $cid => $result) {
77
      if (!$this->valid($cid, $result)) {
78
        // This object has expired, so don't return it.
79 80 81
        unset($results[$cid]);
      }
    }
82 83 84
    // Remove items from the referenced $cids array that we are returning,
    // per the comment in cache_get_multiple() in includes/cache.inc.
    $cids = array_diff($cids, array_keys($results));
85 86 87
    return $results;
  }

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  /**
   * Garbage collection for get() and getMultiple().
   *
   * This functions check for session indicators to NOT use the cache. It
   * checks if the last cache clearing time in there expired and if so unsets
   * them, so anonymous users can get out of the "sessions" after some time, to
   * get fast delivered pages (e.g. from Varnish etc. again).
   */
  protected function garbageCollection() {
    // Clean-up the per-user cache expiration session data, so that the session
    // handler can properly clean-up the session data for anonymous users.
    if (isset($_SESSION['cache_flush'])) {
      $expire = REQUEST_TIME - variable_get('cache_lifetime', 0);
      foreach ($_SESSION['cache_flush'] as $bin => $timestamp) {
        if ($timestamp < $expire) {
          unset($_SESSION['cache_flush'][$bin]);
        }
      }
      if (!$_SESSION['cache_flush']) {
        unset($_SESSION['cache_flush']);
      }
    }
  }

112 113 114 115 116 117 118 119 120 121 122
  /**
   * Checks if a retrieved cache item is valid.
   *
   * @param string $cid
   *   The cache id of the item
   * @param mixed $cache
   *   The cache item.
   *
   * @return bool
   *   Whether the item is valid.
   */
123
  protected function valid($cid, $cache) {
124 125 126
    if ($cache) {
      $cache_tables = isset($_SESSION['cache_flush']) ? $_SESSION['cache_flush'] : NULL;
      // Items that have expired are invalid.
127
      if (isset($cache->expire) && $cache->expire !== CACHE_PERMANENT && $cache->expire <= REQUEST_TIME) {
128 129 130 131
        // If the memcache_stampede_protection variable is set, allow one
        // process to rebuild the cache entry while serving expired content to
        // the rest. Note that core happily returns expired cache items as valid
        // and relies on cron to expire them, but this is mostly reliant on its
132 133 134 135 136
        // use of CACHE_TEMPORARY which does not map well to memcache.
        // @see http://drupal.org/node/534092
        if (variable_get('memcache_stampede_protection', FALSE)) {
          // The process that acquires the lock will get a cache miss, all
          // others will get a cache hit.
137
          if ($this->lockInit() && $this->stampedeProtected($cid) && lock_acquire("memcache_$cid:$this->bin", variable_get('memcache_stampede_semaphore', 15))) {
138 139 140 141 142 143 144 145 146
            $cache = FALSE;
          }
        }
        else {
          $cache = FALSE;
        }
      }
      // Items created before the last full wildcard flush against this bin are
      // invalid.
147
      elseif (!isset($cache->created) || $cache->created <= $this->cache_flush) {
148 149 150 151 152 153 154 155 156 157 158 159 160
        $cache = FALSE;
      }
      // Items created before the last content flush on this bin i.e.
      // cache_clear_all() are invalid.
      elseif ($cache->expire != CACHE_PERMANENT && $cache->created + $this->cache_lifetime <= $this->cache_content_flush) {
        $cache = FALSE;
      }
      // Items cached before the cache was last flushed by the current user are
      // invalid.
      elseif ($cache->expire != CACHE_PERMANENT && is_array($cache_tables) && isset($cache_tables[$this->bin]) && $cache_tables[$this->bin] >= $cache->created) {
        // Cache item expired, return FALSE.
        $cache = FALSE;
      }
161 162 163 164 165 166
      // Temporary items created before the cache was last flushed by
      // cache_clear_all(NULL, $bin) are invalid.
      elseif (!empty($cache->temporary) && $cache->created + $this->cache_lifetime <= $this->cache_temporary_flush) {
        // CACHE_TEMPORARY item expired, return FALSE.
        $cache = FALSE;
      }
167 168
      // Finally, check for wildcard clears against this cid.
      else {
169
        if (!$this->wildcardValid($cid, $cache)) {
170 171
          $cache = FALSE;
        }
172 173 174
      }
    }

175 176 177
    // On cache misses, attempt to avoid stampedes when the
    // memcache_stampede_protection variable is enabled.
    if (!$cache) {
178
      if (variable_get('memcache_stampede_protection', FALSE) && $this->lockInit() && $this->stampedeProtected($cid) && !lock_acquire("memcache_$cid:$this->bin", variable_get('memcache_stampede_semaphore', 15))) {
179 180 181 182 183 184 185 186 187 188 189 190
        // Prevent any single request from waiting more than three times due to
        // stampede protection. By default this is a maximum total wait of 15
        // seconds. This accounts for two possibilities - a cache and lock miss
        // more than once for the same item. Or a cache and lock miss for
        // different items during the same request.
        // @todo: it would be better to base this on time waited rather than
        // number of waits, but the lock API does not currently provide this
        // information. Currently the limit will kick in for three waits of 25ms
        // or three waits of 5000ms.
        static $lock_count = 0;
        $lock_count++;
        if ($lock_count <= variable_get('memcache_stampede_wait_limit', 3)) {
191 192 193
          // The memcache_stampede_semaphore variable was used in previous
          // releases of memcache, but the max_wait variable was not, so by
          // default divide the semaphore value by 3 (5 seconds).
194
          lock_wait("memcache_$cid:$this->bin", variable_get('memcache_stampede_wait_time', 5));
195 196
          $cache = $this->get($cid);
        }
197 198
      }
    }
199 200

    return (bool) $cache;
201
  }
Steve Rude's avatar
Steve Rude committed
202

203 204 205 206
  /**
   * Implements DrupalCacheInterface::set().
   */
  public function set($cid, $data, $expire = CACHE_PERMANENT) {
207
    $created_microtime = round(microtime(TRUE), 3);
208

209
    // Create new cache object.
210
    $cache = new stdClass();
211 212
    $cache->cid = $cid;
    $cache->data = is_object($data) ? clone $data : $data;
213 214
    $cache->created = REQUEST_TIME;
    $cache->created_microtime = $created_microtime;
215
    // Record the previous number of wildcard flushes affecting our cid.
216
    $cache->flushes = $this->wildcardFlushes($cid);
217 218 219
    if ($expire == CACHE_TEMPORARY) {
      // Convert CACHE_TEMPORARY (-1) into something that will live in memcache
      // until the next flush.
220
      $cache->expire = REQUEST_TIME + 2591999;
221 222
      // This is a temporary cache item.
      $cache->temporary = TRUE;
223 224
    }
    // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
225
    elseif ($expire != CACHE_PERMANENT && $expire < 2592000) {
226 227
      // Expire is expressed in seconds, convert to the proper future timestamp
      // as expected in dmemcache_get().
228
      $cache->expire = REQUEST_TIME + $expire;
229
      $cache->temporary = FALSE;
230 231 232
    }
    else {
      $cache->expire = $expire;
233
      $cache->temporary = FALSE;
234
    }
235

236 237 238 239 240 241
    // Manually track the expire time in $cache->expire.  When the object
    // expires, if stampede protection is enabled, it may be served while one
    // process rebuilds it. The ttl sent to memcache is set to the expire twice
    // as long into the future, this allows old items to be expired by memcache
    // rather than evicted along with a sufficient period for stampede
    // protection to continue to work.
242 243 244 245 246 247
    if ($cache->expire == CACHE_PERMANENT) {
      $memcache_expire = $cache->expire;
    }
    else {
      $memcache_expire = $cache->expire + (($cache->expire - REQUEST_TIME) * 2);
    }
248
    dmemcache_set($cid, $cache, $memcache_expire, $this->bin, $this->memcache);
249 250 251 252 253 254

    // Release lock if acquired in $this->valid().
    $lock = "memcache_$cid:$this->bin";
    if (variable_get('memcache_stampede_protection', FALSE) && isset($GLOBALS['locks'][$lock])) {
      lock_release("$lock");
    }
255
  }
256

257 258 259 260
  /**
   * Implements DrupalCacheInterface::clear().
   */
  public function clear($cid = NULL, $wildcard = FALSE) {
261 262 263 264
    if ($this->memcache === FALSE) {
      // No memcache connection.
      return;
    }
265 266 267 268 269

    // It is not possible to detect a cache_clear_all() call other than looking
    // at the backtrace unless http://drupal.org/node/81461 is added.
    $backtrace = debug_backtrace();
    if ($cid == MEMCACHE_CONTENT_CLEAR || (isset($backtrace[2]) && $backtrace[2]['function'] == 'cache_clear_all' && empty($backtrace[2]['args']))) {
270 271
      // Update the timestamp of the last global flushing of this bin.  When
      // retrieving data from this bin, we will compare the cache creation
272 273 274 275 276
      // time minus the cache_flush time to the cache_lifetime to determine
      // whether or not the cached item is still valid.
      $this->cache_content_flush = time();
      $this->variable_set('cache_content_flush_' . $this->bin, $this->cache_content_flush);
      if (variable_get('cache_lifetime', 0)) {
277 278 279
        // We store the time in the current user's session. We then simulate
        // that the cache was flushed for this user by not returning cached
        // data to this user that was cached before the timestamp.
280 281 282 283 284 285 286 287 288 289 290
        if (isset($_SESSION['cache_flush']) && is_array($_SESSION['cache_flush'])) {
          $cache_bins = $_SESSION['cache_flush'];
        }
        else {
          $cache_bins = array();
        }
        // Use time() rather than request time here for correctness.
        $cache_tables[$this->bin] = $this->cache_content_flush;
        $_SESSION['cache_flush'] = $cache_tables;
      }
    }
291
    if (empty($cid) || $wildcard === TRUE) {
292
      // system_cron() flushes all cache bins returned by hook_flush_caches()
293 294 295 296
      // with cache_clear_all(NULL, $bin); The expected behaviour in this case
      // is to perform garbage collection on expired cache items (which is
      // irrelevant to memcache) but also to remove all CACHE_TEMPORARY items.
      // @see https://api.drupal.org/api/drupal/includes!cache.inc/function/cache_clear_all/7
297
      if (!isset($cid)) {
298 299 300 301 302
        // Update the timestamp of the last CACHE_TEMPORARY clear. All
        // temporary cache items created before this will be invalidated.
        $this->cache_temporary_flush = time();
        $this->variable_set("cache_temporary_flush_$this->bin", $this->cache_temporary_flush);
        // Return early here as we do not want to register a wildcard flush.
303 304 305
        return;
      }
      elseif ($cid == '*') {
306 307
        $cid = '';
      }
308
      if (empty($cid)) {
309 310 311 312
        // Update the timestamp of the last global flushing of this bin.  When
        // retrieving data from this bin, we will compare the cache creation
        // time minus the cache_flush time to the cache_lifetime to determine
        // whether or not the cached item is still valid.
313 314 315
        $this->cache_flush = time();
        $this->variable_set("cache_flush_$this->bin", $this->cache_flush);
        $this->flushed = min($this->cache_flush, time() - $this->cache_lifetime);
316

317 318
        if ($this->cache_lifetime) {
          // We store the time in the current user's session which is saved into
319 320 321
          // the sessions table by sess_write().  We then simulate that the
          // cache was flushed for this user by not returning cached data to
          // this user that was cached before the timestamp.
322 323 324 325 326 327 328 329
          if (isset($_SESSION['cache_flush']) && is_array($_SESSION['cache_flush'])) {
            $cache_bins = $_SESSION['cache_flush'];
          }
          else {
            $cache_bins = array();
          }
          $cache_bins[$this->bin] = $this->cache_flush;
          $_SESSION['cache_flush'] = $cache_bins;
330 331 332
        }
      }
      else {
333
        // Register a wildcard flush for current cid.
334
        $this->wildcards($cid, TRUE);
335 336 337 338 339 340
      }
    }
    else {
      $cids = is_array($cid) ? $cid : array($cid);
      foreach ($cids as $cid) {
        dmemcache_delete($cid, $this->bin, $this->memcache);
341 342 343
      }
    }
  }
344

345
  /**
346 347 348 349 350 351 352 353 354 355
   * Sum of all matching wildcards.
   *
   * Checking any single cache item's flush value against this single-value sum
   * tells us whether or not a new wildcard flush has affected the cached item.
   *
   * @param string $cid
   *   The cache id to check.
   *
   * @return int
   *   Sum of all matching wildcards for the given cache id.
356
   */
357
  protected function wildcardFlushes($cid) {
358 359 360 361
    return array_sum($this->wildcards($cid));
  }

  /**
362 363
   * Retrieves all matching wildcards for the given cache id.
   *
364 365 366
   * Utilize multiget to retrieve all possible wildcard matches, storing
   * statically so multiple cache requests for the same item on the same page
   * load doesn't add overhead.
367
   */
368
  protected function wildcards($cid, $flush = FALSE) {
369
    static $wildcards = array();
370
    $matching = array();
371

372 373 374 375
    if (!is_string($cid)) {
      register_shutdown_function('watchdog', 'memcache', 'Invalid cache id received in memcache.inc wildcards() of type !type.', array('!type' => gettype($cid)), WATCHDOG_ERROR);
      return $matching;
    }
376 377
    $length = strlen($cid);

378 379 380 381 382
    if (isset($this->wildcard_flushes[$this->bin]) && is_array($this->wildcard_flushes[$this->bin])) {
      // Wildcard flushes per table are keyed by a substring equal to the
      // shortest wildcard clear on the table so far. So if the shortest
      // wildcard was "links:foo:", and the cid we're checking for is
      // "links:bar:bar", then the key will be "links:bar:".
383 384
      $keys = array_keys($this->wildcard_flushes[$this->bin]);
      $wildcard_length = strlen(reset($keys));
385
      $wildcard_key = substr($cid, 0, $wildcard_length);
386

387 388 389 390 391 392 393
      // Determine which lookups we need to perform to determine whether or not
      // our cid was impacted by a wildcard flush.
      $lookup = array();

      // Find statically cached wildcards, and determine possibly matching
      // wildcards for this cid based on a history of the lengths of past
      // valid wildcard flushes in this bin.
394 395
      if (isset($this->wildcard_flushes[$this->bin][$wildcard_key])) {
        foreach ($this->wildcard_flushes[$this->bin][$wildcard_key] as $flush_length => $timestamp) {
396
          if ($length >= $flush_length && $timestamp >= (REQUEST_TIME - $this->invalidate)) {
397
            $wildcard = '.wildcard-' . substr($cid, 0, $flush_length);
398 399
            if (isset($wildcards[$this->bin][$wildcard])) {
              $matching[$wildcard] = $wildcards[$this->bin][$wildcard];
400 401
            }
            else {
402
              $lookup[$wildcard] = $wildcard;
403
            }
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
          }
        }
      }

      // Do a multi-get to retrieve all possibly matching wildcard flushes.
      if (!empty($lookup)) {
        $values = dmemcache_get_multi($lookup, $this->bin, $this->memcache);
        if (is_array($values)) {
          // Prepare an array of matching wildcards.
          $matching = array_merge($matching, $values);
          // Store matches in the static cache.
          if (isset($wildcards[$this->bin])) {
            $wildcards[$this->bin] = array_merge($wildcards[$this->bin], $values);
          }
          else {
            $wildcards[$this->bin] = $values;
          }
          $lookup = array_diff_key($lookup, $values);
        }
423

424 425
        // Also store failed lookups in our static cache, so we don't have to
        // do repeat lookups on single page loads.
426 427
        foreach ($lookup as $key => $key) {
          $wildcards[$this->bin][$key] = 0;
428
        }
429 430
      }
    }
431

432
    if ($flush) {
433 434 435 436 437
      $key_length = $length;
      if (isset($this->wildcard_flushes[$this->bin])) {
        $keys = array_keys($this->wildcard_flushes[$this->bin]);
        $key_length = strlen(reset($keys));
      }
438
      $key = substr($cid, 0, $key_length);
439 440 441
      // Avoid too many calls to variable_set() by only recording a flush for
      // a fraction of the wildcard invalidation variable, per cid length.
      // Defaults to 28 / 4, or one week.
442
      if (!isset($this->wildcard_flushes[$this->bin][$key][$length]) || (REQUEST_TIME - $this->wildcard_flushes[$this->bin][$key][$length] > $this->invalidate / 4)) {
443

444
        // If there are more than 50 different wildcard keys for this bin
445 446 447 448 449 450 451 452 453 454 455 456 457 458
        // shorten the key by one, this should reduce variability by
        // an order of magnitude and ensure we don't use too much memory.
        if (isset($this->wildcard_flushes[$this->bin]) && count($this->wildcard_flushes[$this->bin]) > 50) {
          $key = substr($cid, 0, $key_length - 1);
          $length = strlen($key);
        }

        // If this is the shortest key length so far, we need to remove all
        // other wildcards lengths recorded so far for this bin and start
        // again. This is equivalent to a full cache flush for this table, but
        // it ensures the minimum possible number of wildcards are requested
        // along with cache consistency.
        if ($length < $key_length) {
          $this->wildcard_flushes[$this->bin] = array();
459 460
          $this->variable_set("cache_flush_$this->bin", time());
          $this->cache_flush = time();
461 462
        }
        $key = substr($cid, 0, $key_length);
463
        $this->wildcard_flushes[$this->bin][$key][$length] = REQUEST_TIME;
464

465
        variable_set('memcache_wildcard_flushes', $this->wildcard_flushes);
466
      }
467
      $key = '.wildcard-' . $cid;
468
      if (isset($wildcards[$this->bin][$key])) {
469
        $wildcards[$this->bin][$key]++;
470 471
      }
      else {
472
        $wildcards[$this->bin][$key] = 1;
473
      }
474
      dmemcache_set($key, $wildcards[$this->bin][$key], 0, $this->bin);
475
    }
476
    return $matching;
477 478 479 480 481
  }

  /**
   * Check if a wildcard flush has invalidated the current cached copy.
   */
482
  protected function wildcardValid($cid, $cache) {
483 484
    // Previously cached content won't have ->flushes defined.  We could
    // force flush, but instead leave this up to the site admin.
485 486
    $flushes = isset($cache->flushes) ? (int) $cache->flushes : 0;
    if ($flushes < (int) $this->wildcardFlushes($cid)) {
487 488 489 490 491
      return FALSE;
    }
    return TRUE;
  }

492 493 494 495
  /**
   * Implements DrupalCacheInterface::isEmpty().
   */
  public function isEmpty() {
496 497
    // We do not know so err on the safe side?
    return FALSE;
498
  }
499

500 501
  /**
   * Helper function to load locking framework if not already loaded.
502 503 504 505
   *
   * @return bool
   *   Whether the locking system was initialized successfully. This must always
   *   return TRUE or throw an exception.
506
   */
507
  public function lockInit() {
508 509 510 511 512 513
    // On a cache miss when page_cache_without_database is enabled, we can end
    // up here without the lock system being initialized. Bootstrap drupal far
    // enough to load the lock system.
    if (!function_exists('lock_acquire')) {
      drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
    }
514

515 516 517 518 519 520
    if (!function_exists('lock_acquire')) {
      // Bootstrap failed, log error.
      register_shutdown_function('watchdog', 'memcache', 'Bootstrap failed in lockInit(), lock_acquire() is not available. (phase:!phase)', array('!phase' => drupal_get_bootstrap_phase()), WATCHDOG_ERROR);
      return FALSE;
    }

521
    return TRUE;
522 523
  }

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  /**
   * Determines whether stampede protection is enabled for a given bin/cid.
   *
   * Memcache stampede protection is primarily designed to benefit the following
   * caching pattern: a miss on a cache_get for a specific cid is immediately
   * followed by a cache_set for that cid. In cases where this pattern is not
   * followed, stampede protection can be disabled to avoid long hanging locks.
   * For example, a cache miss in Drupal core's module_implements() won't
   * execute a cache_set until drupal_page_footer() calls
   * module_implements_write_cache() which can occur much later in page
   * generation.
   *
   * @param string $cid
   *   The cache id of the data to retrieve.
   *
   * @return bool
   *   Returns TRUE if stampede protection is enabled for that particular cache
   *   bin/cid, otherwise FALSE.
   */
  protected function stampedeProtected($cid) {
    $ignore_settings = variable_get('memcache_stampede_protection_ignore', array(
      // Disable stampede protection for specific cids in 'cache_bootstrap'.
      'cache_bootstrap' => array(
        // The module_implements cache is written after finishing the request.
        'module_implements',
        // Variables have their own lock protection.
550
        'variables',
551 552
        // Delayed set.
        'lookup_cache',
553 554 555 556
        // Both schema and the theme_registry uses DrupalCacheArray, which sets
        // the cache entry with a class destructor.
        'schema:runtime:*',
        'theme_registry:runtime:*',
557 558
        // Not written until the end of drupal_page_footer()
        '_drupal_file_scan_cache',
559 560 561
      ),
      // Disable stampede protection for cid prefix in 'cache'.
      'cache' => array(
562
        // I18n uses a class destructor to set the cache.
563 564
        'i18n:string:*',
      ),
565 566
      // Delayed set.
      'cache_path',
567 568 569
      // Disable stampede protection for the contrib cache_rules bin as recent
      // versions of the rules module provides its own stampede protection.
      'cache_rules',
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    ));

    // Support ignoring an entire bin.
    if (in_array($this->bin, $ignore_settings)) {
      return FALSE;
    }

    // Support ignoring by cids.
    if (isset($ignore_settings[$this->bin])) {
      // Support ignoring specific cids.
      if (in_array($cid, $ignore_settings[$this->bin])) {
        return FALSE;
      }
      // Support ignoring cids starting with a suffix.
      foreach ($ignore_settings[$this->bin] as $ignore) {
        $split = explode('*', $ignore);
        if (count($split) > 1 && strpos($cid, $split[0]) === 0) {
          return FALSE;
        }
      }
    }

    return TRUE;
  }

595 596 597 598 599 600
  /**
   * Helper function to reload variables.
   *
   * This is used by the tests to verify that the cache object used the correct
   * settings.
   */
601
  public function reloadVariables() {
602 603 604 605 606
    $this->wildcard_flushes = variable_get('memcache_wildcard_flushes', array());
    $this->invalidate = variable_get('memcache_wildcard_invalidate', MEMCACHE_WILDCARD_INVALIDATE);
    $this->cache_lifetime = variable_get('cache_lifetime', 0);
    $this->cache_flush = variable_get('cache_flush_' . $this->bin);
    $this->cache_content_flush = variable_get('cache_content_flush_' . $this->bin, 0);
607
    $this->cache_temporary_flush = variable_get('cache_temporary_flush_' . $this->bin, 0);
608 609 610 611 612 613
    $this->flushed = min($this->cache_flush, REQUEST_TIME - $this->cache_lifetime);
  }

  /**
   * Re-implementation of variable_set() that writes through instead of clearing.
   */
614
  public function variable_set($name, $value) {
615 616
    global $conf;

617 618 619 620 621
    // If the value appears unchanged, do nothing.
    if (isset($conf[$name]) && $conf[$name] === $value) {
      return;
    }

622
    // When lots of writes happen in a short period of time db_merge can throw
623 624
    // errors. This should only happen if another request has written the
    // variable first, so we catch the error to prevent a fatal error.
625 626 627 628 629 630 631
    try {
      db_merge('variable')
        ->key(array('name' => $name))
        ->fields(array('value' => serialize($value)))
        ->execute();
    }
    catch (Exception $e) {
632 633
      // We can safely ignore the error, since it's likely a cache flush
      // timestamp which should still be accurate.
634 635
    }

636 637
    // If the variables are cached, get a fresh copy, update with the new value
    // and set it again.
638
    if ($cached = cache_get('variables', 'cache_bootstrap')) {
639 640
      $variables = $cached->data;
      $variables[$name] = $value;
641
      cache_set('variables', $variables, 'cache_bootstrap');
642 643 644 645
    }
    // If the variables aren't cached, there's no need to do anything.
    $conf[$name] = $value;
  }
646
}