You are here

function dmemcache_key in Memcache API and Integration 6

Same name and namespace in other branches
  1. 5.2 dmemcache.inc \dmemcache_key()
  2. 5 dmemcache.inc \dmemcache_key()
  3. 7 dmemcache.inc \dmemcache_key()
10 calls to dmemcache_key()
dmemcache_add in ./dmemcache.inc
Add an item into memcache
dmemcache_delete in ./dmemcache.inc
Deletes an item from the cache.
dmemcache_get in ./dmemcache.inc
Retrieve a value from the cache.
dmemcache_get_multi in ./dmemcache.inc
Retrieve multiple values from the cache.
dmemcache_set in ./dmemcache.inc
Place an item into memcache

... See full list

File

./dmemcache.inc, line 758

Code

function dmemcache_key($key = '', $bin = 'cache', $reset = FALSE) {
  static $prefix;
  if (!isset($prefix)) {
    $prefix = '';

    // memcache_key_prefix can be set in settings.php to support site namespaces
    // in a multisite environment.
    if ($prefix = variable_get('memcache_key_prefix', '')) {
      $prefix .= '-';
    }

    // When simpletest is running, emulate the simpletest database prefix here
    // to avoid the child site setting cache entries in the parent site.
    if (isset($GLOBALS['drupal_test_info']['test_run_id'])) {
      $prefix .= $GLOBALS['drupal_test_info']['test_run_id'];
    }
  }
  $full_key = urlencode($prefix . $bin . '-' . $key);

  // Memcache truncates keys longer than 250 characters[*]. This could lead to
  // cache collisions, so we hash keys that are longer than this while still
  // retaining as much of the key bin and name as possible to aid in debugging.
  // The hashing algorithm used is configurable, with sha1 selected by default
  // as it performs quickly with minimal collisions. You can enforce shorter
  // keys by setting memcache_key_max_length in settings.php.
  // [*]https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L47
  $maxlen = variable_get('memcache_key_max_length', 250);
  if (strlen($full_key) > $maxlen) {
    $full_key = urlencode($prefix . $bin) . '-' . hash(variable_get('memcache_key_hash_algorithm', 'sha1'), $key);
    $full_key .= '-' . substr(urlencode($key), 0, $maxlen - 1 - strlen($full_key) - 1);
  }
  return $full_key;
}