You are here

public function DriverBase::stats in Memcache API and Integration 8.2

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.

File

src/Driver/DriverBase.php, line 155

Class

DriverBase
Class DriverBase.

Namespace

Drupal\memcache\Driver

Code

public function stats($stats_bin = 'cache', $stats_type = 'default', $aggregate = FALSE) {

  // 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 = [];
  foreach ($this
    ->getBins() as $bin => $target) {
    if ($stats_bin == $bin) {
      if (isset($this->memcache)) {
        if ($this->memcache instanceof \Memcached) {
          $stats[$bin] = $this->memcache
            ->getStats();
        }
        elseif ($this->memcache instanceof \Memcache) {
          if ($stats_type == 'default' || $stats_type == '') {
            $stats[$bin] = $this->memcache
              ->getExtendedStats();
          }
          elseif (!empty($slab)) {
            $stats[$bin] = $this->memcache
              ->getStats('cachedump', $slab);
          }
          else {
            $stats[$bin] = $this->memcache
              ->getExtendedStats($stats_type);
          }
        }
      }
    }
  }

  // Optionally calculate a sum-total for all servers in the current bin.
  if ($aggregate) {

    // Some variables don't logically aggregate.
    $no_aggregate = [
      'pid',
      'time',
      'version',
      'libevent',
      '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;
                }
              }
            }
          }
        }
      }
    }
  }
  return $stats;
}