You are here

function dmemcache_key in Memcache API and Integration 7

Same name and namespace in other branches
  1. 5.2 dmemcache.inc \dmemcache_key()
  2. 5 dmemcache.inc \dmemcache_key()
  3. 6 dmemcache.inc \dmemcache_key()

Prefixes a key and ensures it is url safe.

Parameters

string $key: The key to prefix and encode.

string $bin: The cache bin which the key applies to.

string $multiple: If TRUE will return all possible prefix variations.

Return value

string or array The prefixed and encoded key(s).

8 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 1074
A memcache API for Drupal.

Code

function dmemcache_key($key, $bin = 'cache', $multiple = FALSE) {
  $prefix = '';
  if ($prefixes = variable_get('memcache_key_prefix', '')) {
    if (is_array($prefixes)) {

      // If no custom prefix defined for bin, use 'default'.
      if (empty($prefixes[$bin])) {
        $bin = 'default';
      }
      if (!empty($prefixes[$bin])) {

        // There can be multiple prefixes specified for each bin.
        if (is_array($prefixes[$bin])) {

          // Optionally return key with all prefixes.
          if ($multiple) {
            $prefix = array();
            foreach ($prefixes[$bin] as $pre) {
              $prefix[] = $pre . '-';
            }
          }
          else {
            $prefix = $prefixes[$bin][0] . '-';
          }
        }
      }
    }
    else {
      $prefix = $prefixes . '-';
    }
  }
  if (!is_array($prefix)) {
    $prefix = array(
      $prefix,
    );
  }
  $full_keys = array();
  foreach ($prefix as $p) {

    // 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'])) {
      $p .= $GLOBALS['drupal_test_info']['test_run_id'];
    }
    $full_keys[] = urlencode($p . $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);
  foreach ($full_keys as $k => $full_key) {
    if (strlen($full_key) > $maxlen) {
      $full_keys[$k] = urlencode($prefix[$k] . $bin) . '-' . hash(variable_get('memcache_key_hash_algorithm', 'sha1'), $key);
      $full_keys[$k] .= '-' . substr(urlencode($key), 0, $maxlen - 1 - strlen($full_keys[$k]) - 1);
    }
  }
  if ($multiple) {

    // An array of prefixed keys.
    return $full_keys;
  }
  else {

    // A single prefixed key.
    return array_shift($full_keys);
  }
}