dmemcache.inc 19.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/*
 * Core dmemcache functions required by:
 *   memcache.inc
 *   memcache.db.inc
 *   session-memcache.inc
 *   session-memcache.db.inc
 */

global $_memcache_statistics;
12
$_memcache_statistics = array();
13 14 15 16 17 18 19 20

/*
 * A memcache API for Drupal.
 */

/**
 *  Place an item into memcache
 *
21
 * @param $key The string with which you will retrieve this item later.
22
 * @param $value The item to be stored.
23 24 25 26 27 28 29 30 31
 * @param $exp Parameter expire is expiration time in seconds. If it's 0, the
 *   item never expires (but memcached server doesn't guarantee this item to be
 *   stored all the time, it could be deleted from the cache to make place for
 *   other items).
 * @param $bin The name of the Drupal subsystem that is making this call.
 *   Examples could be 'cache', 'alias', 'taxonomy term' etc. It is possible to
 *   map different $bin values to different memcache servers.
 * @param $mc Optionally pass in the memcache object.  Normally this value is
 *   determined automatically based on the bin the object is being stored to.
32 33 34
 *
 * @return bool
 */
35
function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) {
36
  global $_memcache_statistics;
37
  $full_key = dmemcache_key($key, $bin);
38 39 40
  if (dmemcache_collect_stats()) {
    $_memcache_statistics[] = array('set', $bin, $full_key, '');
  }
41
  if ($mc || ($mc = dmemcache_object($bin))) {
42
    if ($mc instanceof Memcached) {
43
      return $mc->set($full_key, $value, $exp);
44 45
    }
    else {
46
      return $mc->set($full_key, $value, MEMCACHE_COMPRESSED, $exp);
47 48 49 50 51
    }
  }
  return FALSE;
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 *  Add an item into memcache
 *
 * @param $key The string with which you will retrieve this item later.
 * @param $value The item to be stored.
 * @param $exp Parameter expire is expiration time in seconds. If it's 0, the
 *   item never expires (but memcached server doesn't guarantee this item to be
 *   stored all the time, it could be deleted from the cache to make place for
 *   other items).
 * @param $bin The name of the Drupal subsystem that is making this call.
 *   Examples could be 'cache', 'alias', 'taxonomy term' etc. It is possible
 *   to map different $bin values to different memcache servers.
 * @param $mc Optionally pass in the memcache object.  Normally this value is
 *   determined automatically based on the bin the object is being stored to.
 * @param $flag If using the older memcache PECL extension as opposed to the
 *   newer memcached PECL extension, the MEMCACHE_COMPRESSED flag can be set
 *   to use zlib to store a compressed copy of the item.  This flag option is
 *   completely ignored when using the newer memcached PECL extension.
 *
 * @return bool
 */
73 74
function dmemcache_add($key, $value, $exp = 0, $bin = 'cache', $mc = NULL, $flag = FALSE) {
  global $_memcache_statistics;
75
  $full_key = dmemcache_key($key, $bin);
76 77 78
  if (dmemcache_collect_stats()) {
    $_memcache_statistics[] = array('add', $bin, $full_key, '');
  }
79
  if ($mc || ($mc = dmemcache_object($bin))) {
80
    if ($mc instanceof Memcached) {
Jeremy's avatar
Jeremy committed
81
      return $mc->add($full_key, $value, $exp);
82 83
    }
    else {
Jeremy's avatar
Jeremy committed
84
      return $mc->add($full_key, $value, $flag, $exp);
85 86
    }
  }
87
  return FALSE;
88 89
}

90 91 92 93 94 95 96 97
/**
 * Retrieve a value from the cache.
 *
 * @param $key The key with which the item was stored.
 * @param $bin The bin in which the item was stored.
 *
 * @return The item which was originally saved or FALSE
 */
98
function dmemcache_get($key, $bin = 'cache', $mc = NULL) {
99
  global $_memcache_statistics;
100
  $result = FALSE;
101 102 103
  $full_key = dmemcache_key($key, $bin);
  $statistics = array('get', $bin, $full_key);
  $success = '0';
104 105 106 107 108
  if ($mc || $mc = dmemcache_object($bin)) {
    $track_errors = ini_set('track_errors', '1');
    $php_errormsg = '';

    $result = @$mc->get($full_key);
109 110 111 112
    if (dmemcache_collect_stats()) {
      $statistics[] = (bool) $result;
      $_memcache_statistics[] = $statistics;
    }
113 114 115 116

    if (!empty($php_errormsg)) {
      register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
      $php_errormsg = '';
117
    }
118
    ini_set('track_errors', $track_errors);
119
  }
120

121
  return $result;
122 123 124 125 126 127 128 129 130 131 132 133
}

/**
 * Retrieve multiple values from the cache.
 *
 * @param $keys The keys with which the items were stored.
 * @param $bin The bin in which the item was stored.
 *
 * @return The item which was originally saved or FALSE
 */
function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) {
  global $_memcache_statistics;
134 135 136 137
  $full_keys = array();
  $statistics = array();
  foreach ($keys as $key => $cid) {
    $full_key = dmemcache_key($cid, $bin);
138 139 140
    if (dmemcache_collect_stats()) {
      $statistics[$full_key] = array('getMulti', $bin, $full_key);
    }
141
    $full_keys[$cid] = $full_key;
142
  }
143 144
  $results = array();
  if ($mc || ($mc = dmemcache_object($bin))) {
145
    if ($mc instanceof Memcached) {
146 147
      $results = $mc->getMulti($full_keys);
    }
148
    elseif ($mc instanceof Memcache) {
149 150 151 152 153 154 155 156 157 158
      $track_errors = ini_set('track_errors', '1');
      $php_errormsg = '';

      $results = @$mc->get($full_keys);

      if (!empty($php_errormsg)) {
        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get_multi: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
        $php_errormsg = '';
      }
      ini_set('track_errors', $track_errors);
159 160
    }
  }
161 162 163 164 165
  if (dmemcache_collect_stats()) {
    foreach ($statistics as $key => $values) {
      $values[] = isset($results[$key]) ? '1': '0';
      $_memcache_statistics[] = $values;
    }
166
  }
167 168 169 170 171

  // If $results is FALSE, convert it to an empty array.
  if (!$results) {
    $results = array();
  }
172 173 174

  // Convert the full keys back to the cid.
  $cid_results = array();
175 176 177
  $cid_lookup = array_flip($full_keys);
  foreach ($results as $key => $value) {
    $cid_results[$cid_lookup[$key]] = $value;
178 179
  }
  return $cid_results;
180 181 182 183 184 185 186 187 188 189
}

/**
 * Deletes an item from the cache.
 *
 * @param $key The key with which the item was stored.
 * @param $bin The bin in which the item was stored.
 *
 * @return Returns TRUE on success or FALSE on failure.
 */
190
function dmemcache_delete($key, $bin = 'cache', $mc = NULL) {
191 192
  global $_memcache_statistics;
  $full_key = dmemcache_key($key, $bin);
193 194 195
  if (dmemcache_collect_stats()) {
    $_memcache_statistics[] = array('delete', $bin, $full_key, '');
  }
196
  if ($mc || ($mc = dmemcache_object($bin))) {
197
    return $mc->delete($full_key, 0);
198 199 200 201 202 203 204 205 206 207 208 209 210 211
  }
  return FALSE;
}

/**
 * Immediately invalidates all existing items. dmemcache_flush doesn't actually free any
 * resources, it only marks all the items as expired, so occupied memory will be overwritten by
 * new items.
 *
 * @param $bin The bin to flush. Note that this will flush all bins mapped to the same server
 *   as $bin. There is no way at this time to empty just one bin.
 *
 * @return Returns TRUE on success or FALSE on failure.
 */
212
function dmemcache_flush($bin = 'cache', $mc = NULL) {
213
  global $_memcache_statistics;
214 215 216
  if (dmemcache_collect_stats()) {
    $_memcache_statistics[] = array('flush', $bin, '', '');
  }
217
  if ($mc || ($mc = dmemcache_object($bin))) {
218
    return memcache_flush($mc);
219 220 221
  }
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
function dmemcache_stats($stats_bin = 'cache', $stats_type = 'default', $aggregate = FALSE) {
  $memcache_bins = variable_get('memcache_bins', array('cache' => 'default'));
  // The stats_type can be over-loaded with an integer slab id, if doing a
  // cachedump.  We know we're doing a cachedump if $slab is non-zero.
  $slab = (int)$stats_type;

  foreach ($memcache_bins as $bin => $target) {
    if ($stats_bin == $bin) {
      if ($mc = dmemcache_object($bin)) {
        if ($mc instanceof Memcached) {
          $stats[$bin] = $mc->getStats();
        }
        // The PHP Memcache extension 3.x version throws an error if the stats
        // type is NULL or not in {reset, malloc, slabs, cachedump, items,
        // sizes}. If $stats_type is 'default', then no parameter should be
        // passed to the Memcache memcache_get_extended_stats() function.
        else if ($mc instanceof Memcache) {
          if ($stats_type == 'default' || $stats_type == '') {
            $stats[$bin] = $mc->getExtendedStats();
          }
          // If $slab isn't zero, then we are dumping the contents of a
          // specific cache slab.
          else if (!empty($slab))  {
            $stats[$bin] = $mc->getStats('cachedump', $slab);
          }
          else {
            $stats[$bin] = $mc->getExtendedStats($stats_type);
          }
        }
      }
252
    }
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  }
  // Optionally calculate a sum-total for all servers in the current bin.
  if ($aggregate) {
    // Some variables don't logically aggregate.
    $no_aggregate = array('pid', 'time', 'version', 'pointer_size', 'accepting_conns', 'listen_disabled_num');
    foreach($stats as $bin => $servers) {
      if (is_array($servers)) {
        foreach ($servers as $server) {
          if (is_array($server)) {
            foreach ($server as $key => $value) {
              if (!in_array($key, $no_aggregate)) {
                if (isset($stats[$bin]['total'][$key])) {
                  $stats[$bin]['total'][$key] += $value;
                }
                else {
                  $stats[$bin]['total'][$key] = $value;
                }
              }
            }
          }
        }
      }
275
    }
276
  }
277
  return $stats;
278 279 280
}

/**
281
 * Determine which memcache extension to use: memcache or memcached.
282
 *
283 284
 * By default prefer the 'Memcache' PHP extension, though the default can be
 * overridden by setting memcache_extension in settings.php.
285
 */
286 287 288
function dmemcache_extension() {
  static $extension = NULL;
  if ($extension === NULL) {
289 290 291
    // If an extension is specified in settings.php, use that when available.
    $preferred = variable_get('memcache_extension', NULL);
    if (isset($preferred) && class_exists($preferred)) {
292
      $extension = ucfirst(strtolower($preferred));
293 294 295 296 297 298 299 300
    }
    // If no extension is set, default to Memcache.
    elseif (class_exists('Memcache')) {
      $extension = 'Memcache';
    }
    elseif (class_exists('Memcached')) {
      $extension = 'Memcached';
    }
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    else {
      $extension = FALSE;
    }
  }
  return $extension;
}

/**
 * Return a new memcache instance.
 */
function dmemcache_instance() {
  static $error = FALSE;
  $extension = dmemcache_extension();
  if ($extension == 'Memcache') {
    return new Memcache;
  }
  elseif ($extension == 'Memcached') {
    $memcache = new Memcached;
    $default_opts = array(
      Memcached::OPT_COMPRESSION => FALSE,
      Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
    );
    foreach ($default_opts as $key => $value) {
      $memcache->setOption($key, $value);
    }
    // See README.txt for setting custom Memcache options when using the
    // memcached PECL extension.
    $memconf = variable_get('memcache_options', array());
    foreach ($memconf as $key => $value) {
      $memcache->setOption($key, $value);
    }
    return $memcache;
  }
  else {
    if (!$error) {
      register_shutdown_function('watchdog', 'memcache', 'You must enable the PHP <a href="http://php.net/manual/en/book.memcache.php">memcache</a> (recommended) or <a href="http://php.net/manual/en/book.memcached.php">memcached</a> extension to use memcache.inc.', array(), WATCHDOG_ERROR);
      $error = TRUE;
    }
  }
  return FALSE;
}

/**
 * Initiate a connection to memcache.
 *
 * @param $memcache A memcache instance obtained through dmemcache_instance.
 *
 * @param $server A server string of the format "localhost:11211" or
 *   "unix:///path/to/socket".
 *
 * @connection TRUE or FALSE, whether the $memcache instance already has at
 *   least one open connection.
 *
 * @return TRUE or FALSE if connection was successful.
 */
function dmemcache_connect($memcache, $server, $connection) {
  static $memcache_persistent = NULL;
358

359 360 361 362 363 364 365 366 367
  $extension = dmemcache_extension();

  list($host, $port) = explode(':', $server);

  if ($extension == 'Memcache') {
    // Allow persistent connection via Memcache extension -- note that this
    // module currently doesn't support persistent connections with the
    // Memcached extension. See http://drupal.org/node/822316#comment-4427676
    // for details.
368 369 370
    if (!isset($memcache_persistent)) {
      $memcache_persistent = variable_get('memcache_persistent', FALSE);
    }
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    // Support unix sockets of the format 'unix:///path/to/socket'.
    if ($host == 'unix') {
      // Use full protocol and path as expected by Memcache extension.
      $host = $server;
      $port = 0;
    }

    // When using the PECL memcache extension, we must use ->(p)connect
    // for the first connection.
    if (!$connection) {
      $track_errors = ini_set('track_errors', '1');
      $php_errormsg = '';

      // The Memcache extension requires us to use (p)connect for the first
      // server we connect to.
      if ($memcache_persistent) {
        $rc = @$memcache->pconnect($host, $port);
      }
      else {
        $rc = @$memcache->connect($host, $port);
      }
      if (!empty($php_errormsg)) {
        register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_object: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
        $php_errormsg = '';
      }
      ini_set('track_errors', $track_errors);
    }
    else {
      $rc = $memcache->addServer($host, $port, $memcache_persistent);
    }
  }
  else if ($extension == 'Memcached') {
    // Support unix sockets of the format 'unix:///path/to/socket'.
    if ($host == 'unix') {
      // Strip 'unix://' as expected by Memcached extension.
      $host = substr($server, 7);
      $port = 0;
    }
    $rc = $memcache->addServer($host, $port);
  }
  else {
    $rc = FALSE;
  }
  return $rc;
}

/**
 * Close the connection to the memcache instance.
 */
function dmemcache_close($memcache) {
  $extension = dmemcache_extension();
  if ($extension == 'Memcache' && $memcache instanceof Memcache) {
    $rc = @$memcache->close;
  }
  else if ($extension == 'Memcached' && $memcache instanceof Memcached) {
    $rc = @$memcache->quit;
428
  }
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
  else {
    $rc = FALSE;
  }
  return $rc;
}

/**
 * Return a Memcache object for the specified bin.
 *
 * Note that there is nothing preventing developers from calling this function
 * directly to get the Memcache object. Do this if you need functionality not
 * provided by this API or if you need to use legacy code. Otherwise, use the
 * dmemcache (get, set, delete, flush) API functions provided here.
 *
 * @param $bin The bin which is to be used.
 *
 * @param $flush Rebuild the bin/server/cache mapping.
 *
 * @return a Memcache object or FALSE.
 */
function dmemcache_object($bin = NULL, $flush = FALSE) {
  static $memcacheCache = array();
  static $memcache_servers = array();
  static $memcache_bins = array();
  static $failed_connections = array();
454 455 456

  if ($flush) {
    foreach ($memcacheCache as $cluster) {
457
      memcache_close($cluster);
458 459 460 461
    }
    $memcacheCache = array();
  }

462 463
  $extension = dmemcache_extension();

464 465
  if (empty($memcacheCache) || empty($memcacheCache[$bin])) {
    if (empty($memcache_servers)) {
466
      // Load the variables from settings.php if set.
467
      $memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
468
      $memcache_bins = variable_get('memcache_bins', array('cache' => 'default'));
469 470
    }

471
    // If not manually set, default this cluster to 'default'.
472 473
    $cluster = empty($memcache_bins[$bin]) ? 'default' : $memcache_bins[$bin];

474 475
    // If not manually set, map this bin to 'cache' which maps to the 'default'
    // cluster.
476 477 478 479
    if (empty($memcache_bins[$bin]) && !empty($memcacheCache['cache'])) {
      $memcacheCache[$bin] = &$memcacheCache['cache'];
    }
    else {
480 481
      // Create a new memcache object for each cluster.
      $memcache = dmemcache_instance();
482

483 484
      // Track whether or not we've opened any memcache connections.
      $connection = FALSE;
485

486 487 488 489 490 491 492
      // Link all the servers to this cluster.
      foreach ($memcache_servers as $server => $c) {
        if ($c == $cluster && !isset($failed_connections[$server])) {
          $rc = dmemcache_connect($memcache, $server, $connection);
          if ($rc !== FALSE) {
            // We've made at least one successful connection.
            $connection = TRUE;
493
          }
494
          else {
495 496 497 498 499 500
            // Memcache connection failure. We can't log to watchdog directly
            // because we're in an early Drupal bootstrap phase where watchdog
            // is non-functional. Instead, register a shutdown handler so it
            // gets recorded at the end of the page load.
            register_shutdown_function('watchdog', 'memcache', 'Failed to connect to memcache server: !server', array('!server' => $server), WATCHDOG_ERROR);
            $failed_connections[$server] = FALSE;
501
          }
502 503
        }
      }
504
      if ($connection) {
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        // Map the current bin with the new Memcache object.
        $memcacheCache[$bin] = $memcache;

        // Now that all the servers have been mapped to this cluster, look for
        // other bins that belong to the cluster and map them too.
        foreach ($memcache_bins as $b => $c) {
          if ($c == $cluster && $b != $bin) {
            // Map this bin and cluster by reference.
            $memcacheCache[$b] = &$memcacheCache[$bin];
          }
        }
      }
    }
  }

  return empty($memcacheCache[$bin]) ? FALSE : $memcacheCache[$bin];
}

function dmemcache_key($key, $bin = 'cache') {
524 525 526 527 528 529 530 531
  $prefix = '';
  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'];
532 533 534 535
  }
  $full_key = urlencode($prefix . $bin . '-' . $key);

  // Memcache only supports key lengths up to 250 bytes.  If we have generated
536 537 538
  // a longer key, we shrink it to an acceptible length with a configurable
  // hashing algorithm. Sha1 was selected as the default as it performs
  // quickly with minimal collisions.
539
  if (strlen($full_key) > 250) {
540
    $full_key = urlencode(hash(variable_get('memcache_key_hash_algorithm', 'sha1'), $prefix . $bin . '-' . $key));
541
  }
542

543
  return $full_key;
544
}
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574


/**
 * Checks whether memcache stats need to be collected.
 */
function dmemcache_collect_stats() {
  global $user;
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast = &drupal_static(__FUNCTION__, array('variable_checked' => NULL, 'user_access_checked' => NULL));
  }
  $variable_checked = &$drupal_static_fast['variable_checked'];
  $user_access_checked  = &$drupal_static_fast['user_access_checked'];

  // Confirm DRUPAL_BOOTSTRAP_VARIABLES has been reached. We don't use
  // drupal_get_bootstrap_phase() as it's buggy. We can use variable_get()
  // here because _drupal_bootstrap_variables() includes module.inc
  // immediately after it calls variable_initialize().
  if (!isset($variable_checked) && function_exists('module_list')) {
    $variable_checked = variable_get('show_memcache_statistics', FALSE);
  }
  // If statistics are enabled we need to check user access.
  if (!empty($variable_checked) && !isset($user_access_checked) && !empty($user) && function_exists('user_access')) {
    // Statistics are enabled and the $user object has been populated, so check
    // that the user has access to view them.
    $user_access_checked = user_access('access memcache statistics');
  }
  // Return whether or not statistics are enabled and the user can access them.
  return (!isset($variable_checked) || $variable_checked) && (!isset($user_access_checked) || $user_access_checked);
}