dmemcache.inc 22 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * A memcache API for Drupal.
 *
 * This file contains core dmemcache functions required by:
8
 *   memcache.inc
9 10 11
 *   memcache-lock.inc
 *   memcache-lock-code.inc
 *   memcache-session.inc
12 13
 */

14 15
global $_dmemcache_stats;
$_dmemcache_stats = array('all' => array(), 'ops' => array());
16 17

/**
18
 * Place an item into memcache.
19
 *
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * @param string $key
 *   The string with which you will retrieve this item later.
 * @param mixed $value
 *   The item to be stored.
 * @param int $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 string $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 object $mc
 *   Optionally pass in the memcache object.  Normally this value is determined
 *   automatically based on the bin the object is being stored to.
36 37
 *
 * @return bool
38
 *   TRUE on succes, FALSE otherwise.
39
 */
40
function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) {
41 42
  $collect_stats = dmemcache_stats_init();

43
  $full_key = dmemcache_key($key, $bin);
44 45

  $rc = FALSE;
46
  if ($mc || ($mc = dmemcache_object($bin))) {
47
    if ($mc instanceof Memcached) {
48
      $rc = $mc->set($full_key, $value, $exp);
49 50
    }
    else {
51
      $rc = $mc->set($full_key, $value, MEMCACHE_COMPRESSED, $exp);
52 53
    }
  }
54 55 56 57 58 59

  if ($collect_stats) {
    dmemcache_stats_write('set', $bin, array($full_key => $rc));
  }

  return $rc;
60 61
}

62
/**
63
 * Add an item into memcache.
64
 *
65 66 67 68 69 70
 * @param string $key
 *   The string with which you will retrieve this item later.
 * @param mixed $value
 *   The item to be stored.
 * @param int $exp
 *   Parameter expire is expiration time in seconds. If it's 0, the
71 72 73
 *   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).
74 75
 * @param string $bin
 *   The name of the Drupal subsystem that is making this call.
76 77
 *   Examples could be 'cache', 'alias', 'taxonomy term' etc. It is possible
 *   to map different $bin values to different memcache servers.
78 79
 * @param object $mc
 *   Optionally pass in the memcache object.  Normally this value is
80
 *   determined automatically based on the bin the object is being stored to.
81 82
 * @param int $flag
 *   If using the older memcache PECL extension as opposed to the
83 84 85 86 87
 *   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
88
 *   FALSE if placing the item into memcache failed.
89
 */
90
function dmemcache_add($key, $value, $exp = 0, $bin = 'cache', $mc = NULL, $flag = FALSE) {
91 92
  $collect_stats = dmemcache_stats_init();

93
  $full_key = dmemcache_key($key, $bin);
94 95

  $rc = FALSE;
96
  if ($mc || ($mc = dmemcache_object($bin))) {
97
    if ($mc instanceof Memcached) {
98
      $rc = $mc->add($full_key, $value, $exp);
99 100
    }
    else {
101
      $rc = $mc->add($full_key, $value, $flag, $exp);
102 103
    }
  }
104 105 106 107 108 109

  if ($collect_stats) {
    dmemcache_stats_write('add', $bin, array($full_key => $rc));
  }

  return $rc;
110 111
}

112 113 114
/**
 * Retrieve a value from the cache.
 *
115 116 117 118
 * @param string $key
 *   The key with which the item was stored.
 * @param string $bin
 *   The bin in which the item was stored.
119
 *
120 121
 * @return mixed
 *   The item which was originally saved or FALSE
122
 */
123
function dmemcache_get($key, $bin = 'cache', $mc = NULL) {
124 125
  $collect_stats = dmemcache_stats_init();

126
  $result = FALSE;
127

128
  $full_key = dmemcache_key($key, $bin);
129 130 131 132 133 134 135 136 137
  if ($mc || $mc = dmemcache_object($bin)) {
    $track_errors = ini_set('track_errors', '1');
    $php_errormsg = '';

    $result = @$mc->get($full_key);

    if (!empty($php_errormsg)) {
      register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING);
      $php_errormsg = '';
138
    }
139
    ini_set('track_errors', $track_errors);
140
  }
141

142 143 144 145
  if ($collect_stats) {
    dmemcache_stats_write('get', $bin, array($full_key => (bool) $result));
  }

146
  return $result;
147 148 149 150 151
}

/**
 * Retrieve multiple values from the cache.
 *
152 153 154 155
 * @param array $keys
 *   The keys with which the items were stored.
 * @param string $bin
 *   The bin in which the item was stored.
156
 *
157 158
 * @return mixed
 *   The item which was originally saved or FALSE
159 160
 */
function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) {
161 162 163
  $collect_stats = dmemcache_stats_init();
  $multi_stats = array();

164
  $full_keys = array();
165

166 167
  foreach ($keys as $key => $cid) {
    $full_key = dmemcache_key($cid, $bin);
168
    $full_keys[$cid] = $full_key;
169 170 171 172

    if ($collect_stats) {
      $multi_stats[$full_key] = FALSE;
    }
173
  }
174

175 176
  $results = array();
  if ($mc || ($mc = dmemcache_object($bin))) {
177
    if ($mc instanceof Memcached) {
178 179
      $results = $mc->getMulti($full_keys);
    }
180
    elseif ($mc instanceof Memcache) {
181 182 183 184 185 186 187 188 189 190
      $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);
191 192
    }
  }
193 194 195 196
  // If $results is FALSE, convert it to an empty array.
  if (!$results) {
    $results = array();
  }
197

198 199 200 201 202 203
  if ($collect_stats) {
    foreach ($multi_stats as $key => $value) {
      $multi_stats[$key] = isset($results[$key]) ? TRUE : FALSE;
    }
  }

204 205
  // Convert the full keys back to the cid.
  $cid_results = array();
206 207 208
  $cid_lookup = array_flip($full_keys);
  foreach ($results as $key => $value) {
    $cid_results[$cid_lookup[$key]] = $value;
209
  }
210 211 212 213 214

  if ($collect_stats) {
    dmemcache_stats_write('getMulti', $bin, $multi_stats);
  }

215
  return $cid_results;
216 217 218 219 220
}

/**
 * Deletes an item from the cache.
 *
221 222 223 224
 * @param string $key
 *   The key with which the item was stored.
 * @param string $bin
 *   The bin in which the item was stored.
225
 *
226 227
 * @return bool
 *   Returns TRUE on success or FALSE on failure.
228
 */
229
function dmemcache_delete($key, $bin = 'cache', $mc = NULL) {
230 231
  $collect_stats = dmemcache_stats_init();

232
  $full_key = dmemcache_key($key, $bin);
233 234

  $rc = FALSE;
235
  if ($mc || ($mc = dmemcache_object($bin))) {
236
    $rc = $mc->delete($full_key, 0);
237
  }
238 239 240 241 242 243

  if ($collect_stats) {
    dmemcache_stats_write('delete', $bin, array($full_key => $rc));
  }

  return $rc;
244 245 246
}

/**
247 248 249 250 251
 * Flush all stored items.
 *
 * 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.
252
 *
253 254 255
 * @param string $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.
256
 *
257 258
 * @return bool
 *   Returns TRUE on success or FALSE on failure.
259
 */
260
function dmemcache_flush($bin = 'cache', $mc = NULL) {
261 262 263
  $collect_stats = dmemcache_stats_init();

  $rc = FALSE;
264
  if ($mc || ($mc = dmemcache_object($bin))) {
265 266 267 268 269
    $rc = memcache_flush($mc);
  }

  if ($collect_stats) {
    dmemcache_stats_write('flush', $bin, array('' => $rc));
270
  }
271 272

  return $rc;
273 274
}

275 276 277 278 279 280 281 282 283 284
/**
 * Retrieves statistics recorded during memcache operations.
 *
 * @param string $stats_bin
 *   The bin to retrieve statistics for.
 * @param string $stats_type
 *   The type of statistics to retrieve when using the Memcache extension.
 * @param bool $aggregate
 *   Whether to aggregate statistics.
 */
285 286 287 288
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.
289
  $slab = (int) $stats_type;
290
  $stats = array();
291 292 293 294 295 296 297 298 299 300 301

  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.
302
        elseif ($mc instanceof Memcache) {
303 304 305 306 307
          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.
308
          elseif (!empty($slab)) {
309 310 311 312 313 314 315
            $stats[$bin] = $mc->getStats('cachedump', $slab);
          }
          else {
            $stats[$bin] = $mc->getExtendedStats($stats_type);
          }
        }
      }
316
    }
317 318 319 320
  }
  // Optionally calculate a sum-total for all servers in the current bin.
  if ($aggregate) {
    // Some variables don't logically aggregate.
321 322 323 324 325 326 327 328 329
    $no_aggregate = array(
      'pid',
      'time',
      'version',
      'pointer_size',
      'accepting_conns',
      'listen_disabled_num',
    );
    foreach ($stats as $bin => $servers) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
      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;
                }
              }
            }
          }
        }
      }
346
    }
347
  }
348
  return $stats;
349 350 351
}

/**
352
 * Determine which memcache extension to use: memcache or memcached.
353
 *
354 355
 * By default prefer the 'Memcache' PHP extension, though the default can be
 * overridden by setting memcache_extension in settings.php.
356
 */
357 358 359
function dmemcache_extension() {
  static $extension = NULL;
  if ($extension === NULL) {
360 361 362
    // If an extension is specified in settings.php, use that when available.
    $preferred = variable_get('memcache_extension', NULL);
    if (isset($preferred) && class_exists($preferred)) {
363
      $extension = ucfirst(strtolower($preferred));
364 365 366 367 368 369 370 371
    }
    // If no extension is set, default to Memcache.
    elseif (class_exists('Memcache')) {
      $extension = 'Memcache';
    }
    elseif (class_exists('Memcached')) {
      $extension = 'Memcached';
    }
372 373 374 375 376 377 378 379 380 381 382 383 384 385
    else {
      $extension = FALSE;
    }
  }
  return $extension;
}

/**
 * Return a new memcache instance.
 */
function dmemcache_instance() {
  static $error = FALSE;
  $extension = dmemcache_extension();
  if ($extension == 'Memcache') {
386
    return new Memcache();
387 388
  }
  elseif ($extension == 'Memcached') {
389
    $memcache = new Memcached();
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
    $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.
 *
417 418 419 420
 * @param object $memcache
 *   A memcache instance obtained through dmemcache_instance.
 * @param string $server
 *   A server string of the format "localhost:11211" or
421
 *   "unix:///path/to/socket".
422 423 424
 * @param bool $connection
 *   TRUE or FALSE, whether the $memcache instance already has at least one
 *   open connection.
425
 *
426 427
 * @return bool
 *   TRUE or FALSE if connection was successful.
428 429 430
 */
function dmemcache_connect($memcache, $server, $connection) {
  static $memcache_persistent = NULL;
431

432 433 434 435 436 437 438 439 440
  $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.
441 442 443
    if (!isset($memcache_persistent)) {
      $memcache_persistent = variable_get('memcache_persistent', FALSE);
    }
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    // 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);
    }
  }
476
  elseif ($extension == 'Memcached') {
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    // 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;
  }
499
  elseif ($extension == 'Memcached' && $memcache instanceof Memcached) {
500
    $rc = @$memcache->quit;
501
  }
502 503 504 505 506 507 508 509 510 511 512 513 514 515
  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.
 *
516 517 518 519
 * @param string $bin
 *   The bin which is to be used.
 * @param bool $flush
 *   Defaults to FALSE. Rebuild the bin/server/cache mapping.
520
 *
521 522
 * @return mixed
 *   A Memcache object, or FALSE on failure.
523 524
 */
function dmemcache_object($bin = NULL, $flush = FALSE) {
525
  static $memcache_cache = array();
526 527 528
  static $memcache_servers = array();
  static $memcache_bins = array();
  static $failed_connections = array();
529 530

  if ($flush) {
531
    foreach ($memcache_cache as $cluster) {
532
      memcache_close($cluster);
533
    }
534
    $memcache_cache = array();
535 536
  }

537 538
  $extension = dmemcache_extension();

539
  if (empty($memcache_cache) || empty($memcache_cache[$bin])) {
540
    if (empty($memcache_servers)) {
541
      // Load the variables from settings.php if set.
542
      $memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
543
      $memcache_bins = variable_get('memcache_bins', array('cache' => 'default'));
544 545
    }

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

549 550
    // If not manually set, map this bin to 'cache' which maps to the 'default'
    // cluster.
551 552
    if (empty($memcache_bins[$bin]) && !empty($memcache_cache['cache'])) {
      $memcache_cache[$bin] = &$memcache_cache['cache'];
553 554
    }
    else {
555 556
      // Create a new memcache object for each cluster.
      $memcache = dmemcache_instance();
557

558 559
      // Track whether or not we've opened any memcache connections.
      $connection = FALSE;
560

561 562 563 564 565 566 567
      // 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;
568
          }
569
          else {
570 571 572 573 574 575
            // 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;
576
          }
577 578
        }
      }
579
      if ($connection) {
580
        // Map the current bin with the new Memcache object.
581
        $memcache_cache[$bin] = $memcache;
582 583 584 585 586 587

        // 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.
588
            $memcache_cache[$b] = &$memcache_cache[$bin];
589 590 591 592 593 594
          }
        }
      }
    }
  }

595
  return empty($memcache_cache[$bin]) ? FALSE : $memcache_cache[$bin];
596 597
}

598 599 600 601 602 603 604 605 606 607 608
/**
 * Prefixes a key and ensures it is url safe.
 *
 * @param string $key
 *   The key to prefix and encode.
 * @param string $bin
 *   The cache bin which the key applies to.
 *
 * @return string
 *   The prefixed and encoded key.
 */
609
function dmemcache_key($key, $bin = 'cache') {
610 611 612 613 614 615 616 617
  $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'];
618 619 620 621
  }
  $full_key = urlencode($prefix . $bin . '-' . $key);

  // Memcache only supports key lengths up to 250 bytes.  If we have generated
622 623 624
  // 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.
625
  if (strlen($full_key) > 250) {
626
    $full_key = urlencode(hash(variable_get('memcache_key_hash_algorithm', 'sha1'), $prefix . $bin . '-' . $key));
627
  }
628

629
  return $full_key;
630
}
631 632

/**
633 634
 * Collect statistics if enabled.
 *
635 636 637
 * Optimized function to determine whether or not we should be collecting
 * statistics. Also starts a timer to track how long individual memcache
 * operations take.
638
 *
639 640
 * @return bool
 *   TRUE or FALSE if statistics should be collected.
641
 */
642
function dmemcache_stats_init() {
643
  static $drupal_static_fast;
644

645 646 647 648 649 650 651
  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
652 653 654
  // 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().
655 656 657 658
  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.
659
  if (!empty($variable_checked) && !isset($user_access_checked) && !empty($GLOBALS['user']) && function_exists('user_access')) {
660 661 662 663 664
    // 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.
665 666 667 668 669 670 671 672 673 674 675 676
  if ((!isset($variable_checked) || $variable_checked) && (!isset($user_access_checked) || $user_access_checked)) {
    timer_start('dmemcache');
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Save memcache statistics to be displayed at end of page generation.
 *
677 678 679 680 681 682 683
 * @param string $action
 *   The action being performed (get, set, etc...).
 * @param string $bin
 *   The memcache bin the action is being performed in.
 * @param array $keys
 *   Keyed array in the form (string)$cid => (bool)$success. The keys the
 *   action is being performed on, and whether or not it was a success.
684 685
 */
function dmemcache_stats_write($action, $bin, $keys) {
686
  global $_dmemcache_stats;
687 688 689 690
  // Determine how much time elapsed to execute this action.
  $time = timer_read('dmemcache');
  // Build the 'all' and 'ops' arrays displayed by memcache_admin.module.
  foreach ($keys as $key => $success) {
691 692 693 694 695 696 697
    $_dmemcache_stats['all'][] = array(
      number_format($time, 2),
      $action,
      $bin,
      $key,
      $success ? 'hit' : 'miss',
    );
698 699 700 701
    if (!isset($_dmemcache_stats['ops'][$action])) {
      $_dmemcache_stats['ops'][$action] = array($action, 0, 0, 0);
    }
    $_dmemcache_stats['ops'][$action][1] += $time;
702 703 704 705 706 707
    if ($success) {
      $_dmemcache_stats['ops'][$action][2]++;
    }
    else {
      $_dmemcache_stats['ops'][$action][3]++;
    }
708 709
  }
  // Reset the dmemcache timer for timing the next memcache operation.
710
}