From a51b93cafb331a2a28291d4953ca523c472170e7 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Sun, 20 Mar 2011 08:50:24 -0400
Subject: [PATCH] Issue 1099166 by pillarsdotnet: Simplify dmemcache_stats() and remove unnecessary menu entries from memcache_admin_menu().

---
 dmemcache.inc                        |   31 ++++++++++++-------------------
 memcache_admin/memcache_admin.module |   29 +++++++++++++++--------------
 2 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/dmemcache.inc b/dmemcache.inc
index 5d8d541..c593eea 100644
--- a/dmemcache.inc
+++ b/dmemcache.inc
@@ -177,10 +177,6 @@ function dmemcache_flush($bin = 'cache', $mc = NULL) {
 }
 
 function dmemcache_stats($bin = 'cache', $type = '') {
-  // resolve requests for 'default' type to ''
-  if ($type == 'default') {
-    $type = '';
-  }
   // resolve requests for 'default' bin to 'cache'.
   if ($bin == 'default') {
     $bin = 'cache';
@@ -193,20 +189,17 @@ function dmemcache_stats($bin = 'cache', $type = '') {
     // type is NULL or not in {reset, malloc, slabs, cachedump, items, sizes}.
     // If $type is 'default', then no parameter should be passed to the
     // Memcache memcache_get_extended_stats() function.
-    if ($type == 'default' || $type == '') {
-      if (class_exists('Memcached')) {
-        return $mc->getStats();
-      }
-      else if (class_exists('Memcache')) {
-        return $mc->getExtendedStats();
-      }
-    }
-    else {
-      if (class_exists('Memcached')) {
-        return $mc->getStats();
-      }
-      else if (class_exists('Memcache')) {
-        return $mc->getExtendedStats($type);
+    elseif (class_exists('Memcache')) {
+      switch($type) {
+        case 'cachedump';
+        case 'items':
+        case 'malloc':
+        case 'reset':
+        case 'sizes':
+        case 'slabs':
+          return $mc->getExtendedStats($type);
+        default:
+          return $mc->getExtendedStats();
       }
     }
   }
@@ -271,7 +264,7 @@ function dmemcache_object($bin = NULL, $flush = FALSE) {
           $memcache->setOption($key, $value);
         }
       }
-      else if (class_exists('Memcache')) {
+      elseif (class_exists('Memcache')) {
         $memcache = new Memcache;
       }
       else {
diff --git a/memcache_admin/memcache_admin.module b/memcache_admin/memcache_admin.module
index 5280037..78d84a4 100644
--- a/memcache_admin/memcache_admin.module
+++ b/memcache_admin/memcache_admin.module
@@ -33,6 +33,9 @@ function memcache_admin_perm() {
  * Implementation of hook_menu().
  */
 function memcache_admin_menu() {
+  if (!(class_exists('Memcached') || class_exists('Memcache'))) {
+    return array();
+  }
   $items['admin/config/system/memcache'] = array(
     'title' => 'Memcache',
     'description' => 'Show or hide memcache statistics at the bottom of each page.',
@@ -74,6 +77,10 @@ function memcache_admin_menu() {
       );
       $count++;
 
+      if (class_exists('Memcached')) {
+        continue;
+      }
+
       $sub_count = 0;
       foreach (array('default', 'malloc', 'maps', 'slabs', 'items', 'sizes') as $type) {
         $items['admin/reports/memcache/'. $cluster .'/'. $type] = array(
@@ -105,25 +112,21 @@ function memcache_admin_admin_settings() {
 }
 
 /**
- * Memcahe Stats page
+ * Memcache Stats page
  *
  * @param string $cluster - which cluster to view?
  * @param string $type - which type of stat, eg: default, malloc, maps, cachedump, slabs, items or sizes
  * @return string
  */
 function memcache_admin_stats($cluster = 'default', $type = 'default') {
-  $bin = _memcache_admin_get_bin_for_cluster($cluster);
-
-  if ($bin) {
-    $stats = dmemcache_stats($bin, $type);
-
-    if (is_array($stats) && count($stats)) {
-      $output = '';
-
+  $output = '';
+  if ($bin = _memcache_admin_get_bin_for_cluster($cluster)) {
+    if (is_array($stats = dmemcache_stats($bin, $type)) && count($stats)) {
       foreach ($stats as $server => $values) {
         if (is_array($values)) {
           // Do some custome value tweaks for specific stat page types.
           switch ($type) {
+            case '' :
             case 'default' :
               $values['uptime'] = format_interval($values['uptime']);
               $values['time'] = format_date($values['time']);
@@ -133,8 +136,8 @@ function memcache_admin_stats($cluster = 'default', $type = 'default') {
               $values['limit_maxbytes'] = format_size($values['limit_maxbytes']);
 
               //Custom Entries
-              $values['hit_percentage'] = ($values['cmd_get'] > 0) 
-                ? number_format(100.0 * $values['get_hits'] / $values['cmd_get'], 2) . '%' 
+              $values['hit_percentage'] = ($values['cmd_get'] > 0)
+                ? number_format(100.0 * $values['get_hits'] / $values['cmd_get'], 2) . '%'
                 : '0';
 
               $mem_used = intval($values['bytes']) / (intval($values['limit_maxbytes']) * 1024);
@@ -145,13 +148,11 @@ function memcache_admin_stats($cluster = 'default', $type = 'default') {
           $output .= theme('memcache_admin_stats_table', array('server' => $server, 'stats' => $values));
         }
         else {
-          drupal_set_message(t('Unable to get statistic from server %server', array('%server' => $server)));
+          drupal_set_message(t('Unable to get statistics from server %server', array('%server' => $server)));
         }
       }
     }
-
     else {
-      $output = '';
       drupal_set_message(t('No available statistics for this bin.'));
     }
   }
-- 
1.7.1

