You are here

protected function MemCacheDrupal::stampedeProtected in Memcache API and Integration 7

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.

Parameters

string $cid: The cache id of the data to retrieve.

Return value

bool Returns TRUE if stampede protection is enabled for that particular cache bin/cid, otherwise FALSE.

2 calls to MemCacheDrupal::stampedeProtected()
MemCacheDrupal::valid in ./memcache.inc
Checks if a retrieved cache item is valid.
MockMemCacheDrupal::stampedeProtected in tests/memcache.test
Determines whether stampede protection is enabled for a given bin/cid.
1 method overrides MemCacheDrupal::stampedeProtected()
MockMemCacheDrupal::stampedeProtected in tests/memcache.test
Determines whether stampede protection is enabled for a given bin/cid.

File

./memcache.inc, line 578

Class

MemCacheDrupal
Implementation of cache.inc with memcache logic included

Code

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.
      'variables',
      // Delayed set.
      'lookup_cache',
      // Both schema and the theme_registry uses DrupalCacheArray, which sets
      // the cache entry with a class destructor.
      'schema:runtime:*',
      'theme_registry:runtime:*',
      // Not written until the end of drupal_page_footer()
      '_drupal_file_scan_cache',
    ),
    // Disable stampede protection for cid prefix in 'cache'.
    'cache' => array(
      // I18n uses a class destructor to set the cache.
      'i18n:string:*',
    ),
    // Delayed set.
    'cache_path',
    // Disable stampede protection for the contrib cache_rules bin as recent
    // versions of the rules module provides its own stampede protection.
    'cache_rules',
  ));

  // 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;
}