You are here

function memcache_admin_shutdown in Memcache API and Integration 7

Same name and namespace in other branches
  1. 5 memcache_admin/memcache_admin.module \memcache_admin_shutdown()
  2. 6 memcache_admin/memcache_admin.module \memcache_admin_shutdown()

Displays memcache stats in the footer. This is run as a shutdown function.

See also

memcache_admin_init()

1 string reference to 'memcache_admin_shutdown'
memcache_admin_init in memcache_admin/memcache_admin.module
Implements hook_init().

File

memcache_admin/memcache_admin.module, line 687
For the collection and display of memcache stats.

Code

function memcache_admin_shutdown() {
  global $_dmemcache_stats;

  // Don't call theme() during shutdown if the registry has been rebuilt (such
  // as when enabling/disabling modules on admin/build/modules) as things break.
  // Instead, simply exit without displaying admin statistics for this page
  // load.  See http://drupal.org/node/616282 for discussion.
  if (!function_exists('theme_get_registry') || !theme_get_registry()) {
    return;
  }

  // Try not to break non-HTML pages.
  if (function_exists('drupal_get_http_header')) {
    $header = drupal_get_http_header('content-type');
    if ($header) {
      $formats = array(
        'xml',
        'javascript',
        'json',
        'plain',
        'image',
        'application',
        'csv',
        'x-comma-separated-values',
      );
      foreach ($formats as $format) {
        if (strstr($header, $format)) {
          return;
        }
      }
    }
    else {

      // Workaround for CKEditor, see https://www.drupal.org/node/2556999
      return;
    }
  }
  if (variable_get('show_memcache_statistics', FALSE) && function_exists('user_access') && user_access('access memcache statistics')) {
    $output = '';
    if (!empty($_dmemcache_stats['ops'])) {
      foreach ($_dmemcache_stats['ops'] as $row => $stats) {
        $_dmemcache_stats['ops'][$row][0] = check_plain($stats[0]);
        $_dmemcache_stats['ops'][$row][1] = number_format($stats[1], 2);
        $hits = number_format(_memcache_admin_stats_percent($stats[2], $stats[3]), 1);
        $misses = number_format(_memcache_admin_stats_percent($stats[3], $stats[2]), 1);
        $_dmemcache_stats['ops'][$row][2] = number_format($stats[2]) . " ({$hits}%)";
        $_dmemcache_stats['ops'][$row][3] = number_format($stats[3]) . " ({$misses}%)";
      }
      $variables = array(
        'header' => array(
          t('operation'),
          t('total ms'),
          t('total hits'),
          t('total misses'),
        ),
        'rows' => $_dmemcache_stats['ops'],
      );
      $output .= theme('table', $variables);
    }
    if (!empty($_dmemcache_stats['all'])) {
      foreach ($_dmemcache_stats['all'] as $row => $stats) {
        $_dmemcache_stats['all'][$row][1] = check_plain($stats[1]);
        $_dmemcache_stats['all'][$row][2] = check_plain($stats[2]);
        $_dmemcache_stats['all'][$row][3] = check_plain($stats[3]);
      }
      $variables = array(
        'header' => array(
          t('ms'),
          t('operation'),
          t('bin'),
          t('key'),
          t('status'),
        ),
        'rows' => $_dmemcache_stats['all'],
      );
      $output .= theme('table', $variables);
    }
    if (!empty($output)) {

      // This makes sure all of the HTML is within the <body> even though this
      // <script> is outside it.
      print '<div id="memcache-devel"><h2>' . t('Memcache statistics for @url', array(
        '@url' => current_path(),
      )) . '</h2>' . $output . '</div>';
    }
  }
}