diff -u b/dmemcache.inc b/dmemcache.inc --- b/dmemcache.inc +++ b/dmemcache.inc @@ -2,9 +2,12 @@ /** * @file + * A memcache API for Drupal. + * + * This file contains core dmemcache functions required by: * memcache.inc - * Core dmemcache functions required by: - * memcache.db.inc + * memcache-lock.inc + * memcache-lock-code.inc * memcache-session.inc */ @@ -12,25 +15,27 @@ $_dmemcache_stats = array('all' => array(), 'ops' => array()); /** - * A memcache API for Drupal. - */ - -/** - * Place an item into memcache + * Place an item into memcache. * - * @param $key The string with which you will retrieve this item later. - * @param $value The item to be stored. - * @param $exp Parameter expire is expiration time in seconds. If it's 0, the - * item never expires (but memcached server doesn't guarantee this item to be - * stored all the time, it could be deleted from the cache to make place for - * other items). - * @param $bin The name of the Drupal subsystem that is making this call. - * Examples could be 'cache', 'alias', 'taxonomy term' etc. It is possible to - * map different $bin values to different memcache servers. - * @param $mc Optionally pass in the memcache object. Normally this value is - * determined automatically based on the bin the object is being stored to. + * @param string $key + * The string with which you will retrieve this item later. + * @param mixed $value + * The item to be stored. + * @param int $exp + * Parameter expire is expiration time in seconds. If it's 0, the item never + * expires (but memcached server doesn't guarantee this item to be stored all + * the time, it could be deleted from the cache to make place for other + * items). + * @param string $bin + * The name of the Drupal subsystem that is making this call. Examples could + * be 'cache', 'alias', 'taxonomy term' etc. It is possible to map different + * $bin values to different memcache servers. + * @param object $mc + * Optionally pass in the memcache object. Normally this value is determined + * automatically based on the bin the object is being stored to. * * @return bool + * TRUE on succes, FALSE otherwise. */ function dmemcache_set($key, $value, $exp = 0, $bin = 'cache', $mc = NULL) { $collect_stats = dmemcache_stats_init(); @@ -381,10 +386,10 @@ static $error = FALSE; $extension = dmemcache_extension(); if ($extension == 'Memcache') { - return new Memcache; + return new Memcache(); } elseif ($extension == 'Memcached') { - $memcache = new Memcached; + $memcache = new Memcached(); $default_opts = array( Memcached::OPT_COMPRESSION => FALSE, Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, @@ -412,15 +417,17 @@ /** * Initiate a connection to memcache. * - * @param $memcache A memcache instance obtained through dmemcache_instance. - * - * @param $server A server string of the format "localhost:11211" or + * @param object $memcache + * A memcache instance obtained through dmemcache_instance. + * @param string $server + * A server string of the format "localhost:11211" or * "unix:///path/to/socket". + * @param bool $connection + * TRUE or FALSE, whether the $memcache instance already has at least one + * open connection. * - * @connection TRUE or FALSE, whether the $memcache instance already has at - * least one open connection. - * - * @return TRUE or FALSE if connection was successful. + * @return bool + * TRUE or FALSE if connection was successful. */ function dmemcache_connect($memcache, $server, $connection) { static $memcache_persistent = NULL; @@ -509,11 +516,13 @@ * provided by this API or if you need to use legacy code. Otherwise, use the * dmemcache (get, set, delete, flush) API functions provided here. * - * @param $bin The bin which is to be used. - * - * @param $flush Rebuild the bin/server/cache mapping. + * @param string $bin + * The bin which is to be used. + * @param bool $flush + * Defaults to FALSE. Rebuild the bin/server/cache mapping. * - * @return a Memcache object or FALSE. + * @return mixed + * A Memcache object, or FALSE on failure. */ function dmemcache_object($bin = NULL, $flush = FALSE) { static $memcache_cache = array(); @@ -626,10 +635,12 @@ /** * Collect statistics if enabled. * - * Optimized function to determine whether or not we should be collecting statistics. Also starts a - * timer to track how long individual memcache operations take. + * Optimized function to determine whether or not we should be collecting + * statistics. Also starts a timer to track how long individual memcache + * operations take. * - * @return TRUE or FALSE if statistics should be collected. + * @return bool + * TRUE or FALSE if statistics should be collected. */ function dmemcache_stats_init() { static $drupal_static_fast; @@ -641,9 +652,9 @@ $user_access_checked = &$drupal_static_fast['user_access_checked']; // Confirm DRUPAL_BOOTSTRAP_VARIABLES has been reached. We don't use - // drupal_get_bootstrap_phase() as it's buggy. We can use variable_get() here because - // _drupal_bootstrap_variables() includes module.inc immediately after it calls - // variable_initialize(). + // drupal_get_bootstrap_phase() as it's buggy. We can use variable_get() here + // because _drupal_bootstrap_variables() includes module.inc immediately + // after it calls variable_initialize(). if (!isset($variable_checked) && function_exists('module_list')) { $variable_checked = variable_get('show_memcache_statistics', FALSE); } @@ -666,9 +677,13 @@ /** * Save memcache statistics to be displayed at end of page generation. * - * @param $action The action being performed (get, set, etc...). - * @param $bin The memcache bin the action is being performed in. - * @param $keys The key the action is being performed on, and whether or not it was a success. + * @param string $action + * The action being performed (get, set, etc...). + * @param string $bin + * The memcache bin the action is being performed in. + * @param array $keys + * Keyed array in the form (string)$cid => (bool)$success. The keys the + * action is being performed on, and whether or not it was a success. */ function dmemcache_stats_write($action, $bin, $keys) { global $_dmemcache_stats, $timers; @@ -676,12 +691,23 @@ $time = timer_read('dmemcache'); // Build the 'all' and 'ops' arrays displayed by memcache_admin.module. foreach ($keys as $key => $success) { - $_dmemcache_stats['all'][] = array(number_format($time, 2), $action, $bin, $key, $success ? 'hit' : 'miss'); + $_dmemcache_stats['all'][] = array( + number_format($time, 2), + $action, + $bin, + $key, + $success ? 'hit' : 'miss', + ); if (!isset($_dmemcache_stats['ops'][$action])) { $_dmemcache_stats['ops'][$action] = array($action, 0, 0, 0); } $_dmemcache_stats['ops'][$action][1] += $time; - $success ? $_dmemcache_stats['ops'][$action][2]++ : $_dmemcache_stats['ops'][$action][3]++; + if ($success) { + $_dmemcache_stats['ops'][$action][2]++; + } + else { + $_dmemcache_stats['ops'][$action][3]++; + } } // Reset the dmemcache timer for timing the next memcache operation. unset($timers['dmemcache']); diff -u b/memcache.inc b/memcache.inc --- b/memcache.inc +++ b/memcache.inc @@ -22,10 +22,10 @@ /** * Constructs a MemCacheDrupal object. * - * @param $bin + * @param string $bin * The cache bin for which the object is created. */ - function __construct($bin) { + public function __construct($bin) { $this->memcache = dmemcache_object($bin); $this->bin = $bin; @@ -56,7 +56,7 @@ /** * Implements DrupalCacheInterface::get(). */ - function get($cid) { + public function get($cid) { $cache = dmemcache_get($cid, $this->bin, $this->memcache); return $this->valid($cid, $cache) ? $cache : FALSE; } @@ -64,7 +64,7 @@ /** * Implements DrupalCacheInterface::getMultiple(). */ - function getMultiple(&$cids) { + public function getMultiple(&$cids) { $results = dmemcache_get_multi($cids, $this->bin, $this->memcache); foreach ($results as $cid => $result) { if (!$this->valid($cid, $result)) { @@ -172,16 +172,16 @@ /** * Implements DrupalCacheInterface::set(). */ - function set($cid, $data, $expire = CACHE_PERMANENT) { + public function set($cid, $data, $expire = CACHE_PERMANENT) { $created = time(); // Create new cache object. - $cache = new stdClass; + $cache = new stdClass(); $cache->cid = $cid; $cache->data = is_object($data) ? clone $data : $data; $cache->created = $created; // Record the previous number of wildcard flushes affecting our cid. - $cache->flushes = $this->wildcard_flushes($cid); + $cache->flushes = $this->wildcardFlushes($cid); if ($expire == CACHE_TEMPORARY) { // Convert CACHE_TEMPORARY (-1) into something that will live in memcache // until the next flush. @@ -322,7 +322,7 @@ * @return int * Sum of all matching wildcards for the given cache id. */ - protected function wildcard_flushes($cid) { + protected function wildcardFlushes($cid) { return array_sum($this->wildcards($cid)); } @@ -447,7 +447,7 @@ // Previously cached content won't have ->flushes defined. We could // force flush, but instead leave this up to the site admin. $flushes = isset($cache->flushes) ? (int) $cache->flushes : 0; - if ($flushes < (int) $this->wildcard_flushes($cid)) { + if ($flushes < (int) $this->wildcardFlushes($cid)) { return FALSE; } return TRUE; @@ -464,7 +464,7 @@ /** * Helper function to load locking framework if not already loaded. */ - function lockInit() { + public function lockInit() { // On a cache miss when page_cache_without_database is enabled, we can end // up here without the lock system being initialized. Bootstrap drupal far // enough to load the lock system. diff -u b/memcache.install b/memcache.install --- b/memcache.install +++ b/memcache.install @@ -6,6 +6,38 @@ */ /** + * Implements hook_enable(). + */ +function memcache_enable() { + $error = FALSE; + $memcache = extension_loaded('memcache'); + $memcached = extension_loaded('memcached'); + if (!$memcache && !$memcached) { + $error = TRUE; + } + if (!function_exists('dmemcache_object')) { + // dmemcache.inc isn't loaded. + $error = TRUE; + } + else { + // Make a test connection to all configured memcache servers. + $memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default')); + $memcache = dmemcache_instance(); + foreach ($memcache_servers as $server => $bin) { + if (dmemcache_connect($memcache, $server, FALSE) === FALSE) { + $error = TRUE; + } + else { + dmemcache_close($memcache); + } + } + } + if ($error) { + drupal_set_message(t('There are problems with your Memcache configuration. Please review %readme and visit the Drupal admin !status page for more information.', array('%readme' => 'README.txt', '!status' => l(t('status report'), 'admin/reports/status'))), 'error'); + } +} + +/** * Implements hook_requirements(). */ function memcache_requirements($phase) { diff -u b/memcache.module b/memcache.module --- b/memcache.module +++ b/memcache.module @@ -6,31 +6,4 @@ + * * memcache.inc must be configured in settings.php, and memcache.module is not * necessary to use memcache as a cache backend. */ -function memcache_enable() { - $error = FALSE; - $memcache = extension_loaded('memcache'); - $memcached = extension_loaded('memcached'); - if (!$memcache && !$memcached) { - $error = TRUE; - } - if (!function_exists('dmemcache_object')) { - // dmemcache.inc isn't loaded. - $error = TRUE; - } - else { - // Make a test connection to all configured memcache servers. - $memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default')); - $memcache = dmemcache_instance(); - foreach ($memcache_servers as $server => $bin) { - if (dmemcache_connect($memcache, $server, FALSE) === FALSE) { - $error = TRUE; - } - else { - dmemcache_close($memcache); - } - } - } - if ($error) { - drupal_set_message(t('There are problems with your Memcache configuration. Please review %readme and visit the Drupal admin !status page for more information.', array('%readme' => 'README.txt', '!status' => l(t('status report'), 'admin/reports/status'))), 'error'); - } -} diff -u b/memcache_admin/memcache_admin.module b/memcache_admin/memcache_admin.module --- b/memcache_admin/memcache_admin.module +++ b/memcache_admin/memcache_admin.module @@ -2,14 +2,19 @@ /** * @file - * For the collection of memcache stats. This small .js file makes sure that the - * HTML displaying the stats is inside of the part of the HTML - * document. + * For the collection and display of memcache stats. + * + * This module adds a small .js file to makes sure that the HTML displaying the + * stats is inside of the part of the HTML document. + */ + +/** + * Implements hook_init(). */ function memcache_admin_init() { global $user; if (($user->uid == 0) || strstr($_SERVER['PHP_SELF'], 'update.php') || (isset($_GET['q']) && (in_array($_GET['q'], array('upload/js', 'admin/content/node-settings/rebuild')) || substr($_GET['q'], 0, strlen('system/files')) == 'system/files' || substr($_GET['q'], 0, strlen('batch')) == 'batch' || strstr($_GET['q'], 'autocomplete')))) { - // update.php relies on standard error handler + // update.php relies on standard error handler. } else { if ($user->uid) { @@ -71,7 +76,7 @@ $items["admin/reports/memcache/$cluster"] = array( 'title' => $cluster, - 'type' => $count == 0 ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, + 'type' => $count == 0 ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, 'page callback' => 'memcache_admin_stats', 'page arguments' => array($cluster), 'access arguments' => array('access memcache statistics'), @@ -80,7 +85,7 @@ foreach ($cluster_info['servers'] as $server) { $items["admin/reports/memcache/$cluster/$server"] = array( 'title' => check_plain($server), - 'type' => MENU_CALLBACK, + 'type' => MENU_CALLBACK, 'page callback' => 'memcache_admin_stats_raw', 'page arguments' => array($cluster, $server), 'access arguments' => array('access memcache statistics'), @@ -176,7 +181,12 @@ if (!is_array($stats)) { $stats = array(); } - $stats += array('incr_hits' => 0, 'incr_misses' => 0, 'decr_hits' => 0, 'decr_misses' => 0); + $stats += array( + 'incr_hits' => 0, + 'incr_misses' => 0, + 'decr_hits' => 0, + 'decr_misses' => 0, + ); return t('!incr increments, !decr decrements', array('!incr' => number_format($stats['incr_hits'] + $stats['incr_misses']), '!decr' => number_format($stats['decr_hits'] + $stats['decr_misses']))); } @@ -190,7 +200,7 @@ else { $written = $stats['bytes_read'] / $stats['bytes_written'] * 100; } - return t('!to:!from (!written% to cache)', array('!to' => format_size((int)$stats['bytes_read']), '!from' => format_size((int)$stats['bytes_written']), '!written' => number_format($written, 2))); + return t('!to:!from (!written% to cache)', array('!to' => format_size((int) $stats['bytes_read']), '!from' => format_size((int) $stats['bytes_written']), '!written' => number_format($written, 2))); } /** @@ -240,9 +250,10 @@ } /** - * Memcache Stats page + * Callback for the Memcache Stats page. * * @return string + * The page output. */ function memcache_admin_stats($bin = 'default') { $bin = memcache_admin_bin_mapping($bin); @@ -351,7 +362,11 @@ $data['memory_evictions']), ), ); - $output = theme('memcache_admin_stats_table', array('bin' => $bin, 'servers' => $servers, 'report' => $report)); + $output = theme('memcache_admin_stats_table', array( + 'bin' => $bin, + 'servers' => $servers, + 'report' => $report, + )); } else { $output = ''; @@ -366,27 +381,48 @@ */ function memcache_admin_stats_raw($bin, $server, $type = 'default') { $cluster = memcache_admin_bin_mapping($bin); - $slab = (int)arg(7); + $slab = (int) arg(7); if (arg(6) == 'cachedump' && !empty($slab) && user_access('access slab cachedump')) { $stats = dmemcache_stats($cluster, arg(7), FALSE); } else { $stats = dmemcache_stats($cluster, $type, FALSE); } - $breadcrumbs = array(l(t('Home'), NULL), l(t('Administer'), 'admin'), l(t('Reports'), 'admin/reports'), l(t('Memcache'), 'admin/reports/memcache'), l(t($bin), "admin/reports/memcache/$bin")); + $breadcrumbs = array( + l(t('Home'), NULL), + l(t('Administer'), 'admin'), + l(t('Reports'), 'admin/reports'), + l(t('Memcache'), 'admin/reports/memcache'), + l(t($bin), "admin/reports/memcache/$bin"), + ); if ($type == 'slabs' && arg(6) == 'cachedump' && user_access('access slab cachedump')) { $breadcrumbs[] = l($server, "admin/reports/memcache/$bin/$server"); $breadcrumbs[] = l(t('slabs'), "admin/reports/memcache/$bin/$server/$type"); } drupal_set_breadcrumb($breadcrumbs); if (isset($stats[$cluster][$server]) && is_array($stats[$cluster][$server]) && count($stats[$cluster][$server])) { - $output = theme('memcache_admin_stats_raw_table', array('cluster' => $cluster, 'server' => $server, 'stats' => $stats[$cluster][$server], 'type' => $type)); + $output = theme('memcache_admin_stats_raw_table', array( + 'cluster' => $cluster, + 'server' => $server, + 'stats' => $stats[$cluster][$server], + 'type' => $type, + )); } elseif ($type == 'slabs' && is_array($stats[$cluster]) && count($stats[$cluster])) { - $output = theme('memcache_admin_stats_raw_table', array('cluster' => $cluster, 'server' => $server, 'stats' => $stats[$cluster], 'type' => $type)); + $output = theme('memcache_admin_stats_raw_table', array( + 'cluster' => $cluster, + 'server' => $server, + 'stats' => $stats[$cluster], + 'type' => $type, + )); } else { - $output = theme('memcache_admin_stats_raw_table', array('cluster' => $cluster, 'server' => $server, 'stats' => array(), 'type' => $type)); + $output = theme('memcache_admin_stats_raw_table', array( + 'cluster' => $cluster, + 'server' => $server, + 'stats' => array(), + 'type' => $type, + )); drupal_set_message(t('No @type statistics for this bin.', array('@type' => $type))); } return $output; @@ -398,16 +434,25 @@ function memcache_admin_theme() { return array( 'memcache_admin_stats_table' => array( - 'variables' => array('bin' => NULL, 'servers' => NULL, 'report' => NULL), + 'variables' => array( + 'bin' => NULL, + 'servers' => NULL, + 'report' => NULL, + ), ), 'memcache_admin_stats_raw_table' => array( - 'variables' => array('bin' => NULL, 'server' => NULL, 'stats' => NULL, 'type' => NULL), - ) + 'variables' => array( + 'bin' => NULL, + 'server' => NULL, + 'stats' => NULL, + 'type' => NULL, + ), + ), ); } /** - * Theme function for rendering the output from memcache_admin_stats + * Theme function for rendering the output from memcache_admin_stats. */ function theme_memcache_admin_stats_table($variables) { $bin = $variables['bin']; @@ -466,7 +511,7 @@ $memcache_bins = variable_get('memcache_bins', array()); $bin = isset($memcache_bins[$cluster]) ? $memcache_bins[$cluster] : 'default'; - // Provide navigation for the various memcache stats types + // Provide navigation for the various memcache stats types. if (count(memcache_admin_stats_types($bin)) > 1) { foreach (memcache_admin_stats_types($bin) as $type) { if ($current_type == $type) { @@ -487,7 +532,7 @@ $stats = $stats['items']; } foreach ($stats as $key => $value) { - // Add navigation for getting a cachedump of individual slabs + // Add navigation for getting a cachedump of individual slabs. if (($current_type == 'slabs' || $current_type == 'items') && is_int($key) && user_access('access slab cachedump')) { $key = l($key, "admin/reports/memcache/$bin/$server/slabs/cachedump/$key"); } @@ -523,10 +568,13 @@ } /** - * Retrieve the cluster for any given bin + * Retrieve the bin for any given cluster. + * + * @param string $cluster + * Cluster ID * - * @param string $cluster - Cluster ID * @return string + * The name of the bin. */ function _memcache_admin_get_bin_for_cluster($cluster) { static $cluster_map = array(); @@ -560,8 +608,9 @@ } /** - * See memcache_admin_init() which registers this function as a shutdown function. - * Displays memcache stats in the footer. + * Displays memcache stats in the footer. This is run as a shutdown function. + * + * @see memcache_admin_init() */ function memcache_admin_shutdown() { global $_dmemcache_stats; @@ -578,7 +627,16 @@ 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'); + $formats = array( + 'xml', + 'javascript', + 'json', + 'plain', + 'image', + 'application', + 'csv', + 'x-comma-separated-values', + ); foreach ($formats as $format) { if (strstr($header, $format)) { return; @@ -598,8 +656,15 @@ $_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']); + $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'])) { @@ -612,12 +677,20 @@ - $variables = array('header' => array(t('ms'), t('operation'), t('bin'), t('key'), t('status')), - 'rows' => $_dmemcache_stats['all']); + $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 even though this