diff --git a/dmemcache.inc b/dmemcache.inc index 5b0629a..47f7ac9 100644 --- a/dmemcache.inc +++ b/dmemcache.inc @@ -35,7 +35,9 @@ $_memcache_statistics = array(); function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) { global $_memcache_statistics; $full_key = dmemcache_key($key, $bin); - $_memcache_statistics[] = array('set', $bin, $full_key, ''); + if (dmemcache_collect_stats()) { + $_memcache_statistics[] = array('set', $bin, $full_key, ''); + } if ($mc || ($mc = dmemcache_object($bin))) { if ($mc instanceof Memcached) { return $mc->set($full_key, $value, $exp); @@ -71,7 +73,9 @@ function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) { function dmemcache_add($key, $value, $exp = 0, $bin = 'cache', $mc = NULL, $flag = FALSE) { global $_memcache_statistics; $full_key = dmemcache_key($key, $bin); - $_memcache_statistics[] = array('add', $bin, $full_key, ''); + if (dmemcache_collect_stats()) { + $_memcache_statistics[] = array('add', $bin, $full_key, ''); + } if ($mc || ($mc = dmemcache_object($bin))) { if ($mc instanceof Memcached) { return $mc->add($full_key, $value, $exp); @@ -95,15 +99,15 @@ function dmemcache_get($key, $bin = 'cache', $mc = NULL) { global $_memcache_statistics; $result = FALSE; $full_key = dmemcache_key($key, $bin); - $statistics = array('get', $bin, $full_key); $success = '0'; if ($mc || $mc = dmemcache_object($bin)) { $track_errors = ini_set('track_errors', '1'); $php_errormsg = ''; $result = @$mc->get($full_key); - $statistics[] = (bool) $result; - $_memcache_statistics[] = $statistics; + if (dmemcache_collect_stats()) { + $_memcache_statistics[] = array('get', $bin, $full_key, (bool)$result); + } if (!empty($php_errormsg)) { register_shutdown_function('watchdog', 'memcache', 'Exception caught in dmemcache_get: !msg', array('!msg' => $php_errormsg), WATCHDOG_WARNING); @@ -129,7 +133,9 @@ function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) { $statistics = array(); foreach ($keys as $key => $cid) { $full_key = dmemcache_key($cid, $bin); - $statistics[$full_key] = array('getMulti', $bin, $full_key); + if (dmemcache_collect_stats()) { + $statistics[$full_key] = array('getMulti', $bin, $full_key); + } $full_keys[$cid] = $full_key; } $results = array(); @@ -150,9 +156,11 @@ function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) { ini_set('track_errors', $track_errors); } } - foreach ($statistics as $key => $values) { - $values[] = isset($results[$key]) ? '1': '0'; - $_memcache_statistics[] = $values; + if (dmemcache_collect_stats()) { + foreach ($statistics as $key => $values) { + $values[] = isset($results[$key]) ? '1': '0'; + $_memcache_statistics[] = $values; + } } // If $results is FALSE, convert it to an empty array. @@ -180,7 +188,9 @@ function dmemcache_get_multi($keys, $bin = 'cache', $mc = NULL) { function dmemcache_delete($key, $bin = 'cache', $mc = NULL) { global $_memcache_statistics; $full_key = dmemcache_key($key, $bin); - $_memcache_statistics[] = array('delete', $bin, $full_key, ''); + if (dmemcache_collect_stats()) { + $_memcache_statistics[] = array('delete', $bin, $full_key, ''); + } if ($mc || ($mc = dmemcache_object($bin))) { return $mc->delete($full_key, 0); } @@ -199,7 +209,9 @@ function dmemcache_delete($key, $bin = 'cache', $mc = NULL) { */ function dmemcache_flush($bin = 'cache', $mc = NULL) { global $_memcache_statistics; - $_memcache_statistics[] = array('flush', $bin, '', ''); + if (dmemcache_collect_stats()) { + $_memcache_statistics[] = array('flush', $bin, '', ''); + } if ($mc || ($mc = dmemcache_object($bin))) { return memcache_flush($mc); } @@ -458,3 +470,32 @@ function dmemcache_key($key, $bin = 'cache') { return $full_key; } + +/** + * Checks whether memcache stats need to be collected. + */ +function dmemcache_collect_stats() { + static $variable_get, $user_access, $module_exists; + + // _drupal_bootstrap_variables() includes module.inc immediately after + // it calls variable_initialize(), so we're safe to use + // variable_get() here. If show_memcache_statistics is set in + // settings.php it should be available eary in the bootstrap so also + // check this. + global $conf; + if (!isset($variable_get) && (function_exists('module_list') || isset($conf['show_memcache_statistics']))) { + $variable_get = variable_get('show_memcache_statistics', TRUE); + } + // cache_get() / cache_set() may be called during module bootstrap so we need + // to wait for boostrap to finish to avoid an infinite loop. We can't use + // drupal_get_bootstrap_phase() as it has a bug, so check for global $theme. + elseif (!empty($variable_get) && !isset($module_exists) && !empty($GLOBALS['theme'])) { + $module_exists = module_exists('memcache_admin'); + // If the module doesn't exist then the user can't have access. + $user_access = $module_exists && user_access('access memcache statistics'); + } + + return (!isset($variable_get) || $variable_get) + && (!isset($module_exists) || $module_exists) + && (!isset($user_access) || $user_access); +} diff --git a/memcache_admin/memcache_admin.install b/memcache_admin/memcache_admin.install index 0622b20..0f2652e 100644 --- a/memcache_admin/memcache_admin.install +++ b/memcache_admin/memcache_admin.install @@ -11,3 +11,10 @@ function memcache_admin_update_7001() { drupal_flush_all_caches(); menu_rebuild(); } + +/** + * Implements hook_uninstall(). + */ +function memcache_admin_uninstall() { + variable_del('show_memcache_statistics'); +} diff --git a/memcache_admin/memcache_admin.module b/memcache_admin/memcache_admin.module index 9760668..95496a0 100644 --- a/memcache_admin/memcache_admin.module +++ b/memcache_admin/memcache_admin.module @@ -104,9 +104,10 @@ function memcache_admin_menu() { * Settings form. */ function memcache_admin_admin_settings() { - $form['show_memcache_statistics'] = array('#type' => 'checkbox', + $form['show_memcache_statistics'] = array( + '#type' => 'checkbox', '#title' => t('Show memcache statistics at the bottom of each page'), - '#default_value' => variable_get('show_memcache_statistics', 1), + '#default_value' => variable_get('show_memcache_statistics', TRUE), '#description' => t("These statistics will be visible to users with the 'access memcache statistics' permission."), ); return system_settings_form($form); @@ -433,7 +434,7 @@ function memcache_admin_stats_types($bin) { return array(); } } - + function theme_memcache_admin_stats_raw_table($variables) { $cluster = $variables['cluster']; $server = $variables['server'];