You are here

function dmemcache_stats in Memcache API and Integration 7

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

Retrieves statistics recorded during memcache operations.

Parameters

string $stats_bin: The bin to retrieve statistics for.

string $stats_type: The type of statistics to retrieve when using the Memcache extension.

bool $aggregate: Whether to aggregate statistics.

Return value

array The statistics

5 calls to dmemcache_stats()
drush_memcache_stats in ./memcache.drush.inc
Display memcache statistics.
memcache_admin_stats in memcache_admin/memcache_admin.module
Callback for the Memcache Stats page.
memcache_admin_stats_raw in memcache_admin/memcache_admin.module
Callback for the server statistics page.
theme_memcache_admin_stats_raw_table in memcache_admin/memcache_admin.module
Theme function to produce a table of statistics.
_memcached_ascii_auth_version_valid in ./memcache.install
If ASCII protocol authentication is enabled, validate whether the current memcached version meets the minimum and/or recommended requirements.

File

./dmemcache.inc, line 626
A memcache API for Drupal.

Code

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;
  $stats = array();
  foreach ($memcache_bins as $bin => $target) {
    if ($stats_bin == $bin) {
      if ($mc = dmemcache_object($bin)) {
        if ($mc instanceof Memcached) {
          $stats[$bin] = $mc
            ->getStats();

          // If a composite call fails, we need to reset the authentication
          // for the whole mc object.
          if (empty($stats[$bin]) && _dmemcache_reset_ascii_auth($mc)) {
            $stats[$bin] = $mc
              ->getStats();
          }
        }
        elseif ($mc instanceof Memcache) {
          if ($stats_type == 'default' || $stats_type == '') {
            $rc = $mc
              ->getExtendedStats();
            if (is_array($rc)) {
              foreach ($rc as $server => $data) {
                if (empty($data)) {
                  unset($rc[$server]);
                }
              }
              if (!empty($rc)) {
                $stats[$bin] = $rc;
              }
            }
          }
          elseif (!empty($slab)) {
            $stats[$bin] = $mc
              ->getStats('cachedump', $slab);
          }
          else {
            $stats[$bin] = $mc
              ->getExtendedStats($stats_type);
          }
        }
      }
    }
  }

  // Optionally calculate a sum-total for all servers in the current bin.
  if ($aggregate) {
    foreach ($stats as $bin => $servers) {
      if (is_array($servers)) {
        foreach ($servers as $server) {
          if (is_array($server)) {
            foreach ($server as $key => $value) {
              if (is_numeric($value)) {
                if (isset($stats[$bin]['total'][$key])) {
                  $stats[$bin]['total'][$key] += $value;
                }
                else {
                  $stats[$bin]['total'][$key] = $value;
                }
              }
            }
          }
        }
      }
    }
  }
  return $stats;
}