From: Aaron Schulz Date: Thu, 15 Oct 2015 02:45:03 +0000 (-0700) Subject: Convert some users to WANObjectCache for consistency X-Git-Tag: 1.31.0-rc.0~9349^2 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=4f781c7bf9f8ef874bf971ab0d853d83672a942a;p=lhc%2Fweb%2Fwiklou.git Convert some users to WANObjectCache for consistency These callers don't need to do purges, but can still perfectly take advantage of this instance over a plain BagOStuff. Namely: * Replication and snapshot lag awareness * Preemptive regeneration * Easy process cache support The idea is for there to only be one caching class/factory to use, instead of having rules for picking which one to use. Change-Id: I8e362df451c0c28731fc853c044c4c4b8e097f01 --- diff --git a/includes/AjaxResponse.php b/includes/AjaxResponse.php index 6c2efc296a..34bb65f19b 100644 --- a/includes/AjaxResponse.php +++ b/includes/AjaxResponse.php @@ -276,13 +276,11 @@ class AjaxResponse { * @return bool */ function loadFromMemcached( $mckey, $touched ) { - global $wgMemc; - if ( !$touched ) { return false; } - $mcvalue = $wgMemc->get( $mckey ); + $mcvalue = ObjectCache::getMainWANInstance()->get( $mckey ); if ( $mcvalue ) { # Check to see if the value has been invalidated if ( $touched <= $mcvalue['timestamp'] ) { @@ -304,9 +302,7 @@ class AjaxResponse { * @return bool */ function storeInMemcached( $mckey, $expiry = 86400 ) { - global $wgMemc; - - $wgMemc->set( $mckey, + ObjectCache::getMainWANInstance()->set( $mckey, array( 'timestamp' => wfTimestampNow(), 'value' => $this->mText diff --git a/includes/Block.php b/includes/Block.php index 0ec4ad1400..5dee23ea4a 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -679,16 +679,17 @@ class Block { * @return bool */ public static function isWhitelistedFromAutoblocks( $ip ) { - global $wgMemc; - // Try to get the autoblock_whitelist from the cache, as it's faster // than getting the msg raw and explode()'ing it. - $key = wfMemcKey( 'ipb', 'autoblock', 'whitelist' ); - $lines = $wgMemc->get( $key ); - if ( !$lines ) { - $lines = explode( "\n", wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() ); - $wgMemc->set( $key, $lines, 3600 * 24 ); - } + + $lines = ObjectCache::getMainWANInstance()->getWithSetCallback( + wfMemcKey( 'ipb', 'autoblock', 'whitelist' ), + 86400, + function () { + return explode( "\n", + wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() ); + } + ); wfDebug( "Checking the autoblock whitelist..\n" ); diff --git a/includes/Revision.php b/includes/Revision.php index 24c025f6c0..a498817c9b 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -1530,12 +1530,13 @@ class Revision implements IDBAccessObject { */ protected function loadText() { // Caching may be beneficial for massive use of external storage - global $wgRevisionCacheExpiry, $wgMemc; + global $wgRevisionCacheExpiry; + $cache = ObjectCache::getMainWANInstance(); $textId = $this->getTextId(); $key = wfMemcKey( 'revisiontext', 'textid', $textId ); if ( $wgRevisionCacheExpiry ) { - $text = $wgMemc->get( $key ); + $text = $cache->get( $key ); if ( is_string( $text ) ) { wfDebug( __METHOD__ . ": got id $textId from cache\n" ); return $text; @@ -1582,7 +1583,7 @@ class Revision implements IDBAccessObject { # No negative caching -- negative hits on text rows may be due to corrupted slave servers if ( $wgRevisionCacheExpiry && $text !== false ) { - $wgMemc->set( $key, $text, $wgRevisionCacheExpiry ); + $cache->set( $key, $text, $wgRevisionCacheExpiry ); } return $text; diff --git a/includes/SiteStats.php b/includes/SiteStats.php index 76e7f7eb9f..b84d2bfa95 100644 --- a/includes/SiteStats.php +++ b/includes/SiteStats.php @@ -180,23 +180,23 @@ class SiteStats { * @return int */ static function numberingroup( $group ) { - if ( !isset( self::$groupMemberCounts[$group] ) ) { - global $wgMemc; - $key = wfMemcKey( 'SiteStats', 'groupcounts', $group ); - $hit = $wgMemc->get( $key ); - if ( !$hit ) { + return ObjectCache::getMainWANInstance()->getWithSetCallback( + wfMemcKey( 'SiteStats', 'groupcounts', $group ), + 3600, + function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) { $dbr = wfGetDB( DB_SLAVE ); - $hit = $dbr->selectField( + + $setOpts += $dbr->getCacheSetOptions(); + + return $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => $group ), __METHOD__ ); - $wgMemc->set( $key, $hit, 3600 ); - } - self::$groupMemberCounts[$group] = $hit; - } - return self::$groupMemberCounts[$group]; + }, + array( 'pcTTL' => 10 ) + ); } /** diff --git a/includes/api/ApiHelp.php b/includes/api/ApiHelp.php index abec828f1b..c33b27bc1b 100644 --- a/includes/api/ApiHelp.php +++ b/includes/api/ApiHelp.php @@ -91,7 +91,7 @@ class ApiHelp extends ApiBase { * @return string */ public static function getHelp( IContextSource $context, $modules, array $options ) { - global $wgMemc, $wgContLang; + global $wgContLang; if ( !is_array( $modules ) ) { $modules = array( $modules ); @@ -104,6 +104,7 @@ class ApiHelp extends ApiBase { } $out->setPageTitle( $context->msg( 'api-help-title' ) ); + $cache = ObjectCache::getMainWANInstance(); $cacheKey = null; if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain && $options['recursivesubmodules'] && $context->getLanguage() === $wgContLang @@ -114,7 +115,7 @@ class ApiHelp extends ApiBase { $cacheKey = wfMemcKey( 'apihelp', $modules[0]->getModulePath(), (int)!empty( $options['toc'] ), str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) ); - $cached = $wgMemc->get( $cacheKey ); + $cached = $cache->get( $cacheKey ); if ( $cached ) { $out->addHTML( $cached ); return; @@ -151,7 +152,7 @@ class ApiHelp extends ApiBase { $out->addHTML( $html ); if ( $cacheKey !== null ) { - $wgMemc->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout ); + $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout ); } } diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 5d2db476c5..c641c9513c 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -1596,25 +1596,19 @@ class ApiMain extends ApiBase { */ public function makeHelpMsg() { wfDeprecated( __METHOD__, '1.25' ); - global $wgMemc; - $this->setHelp(); - // Get help text from cache if present - $key = wfMemcKey( 'apihelp', $this->getModuleName(), - str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) ); + $this->setHelp(); $cacheHelpTimeout = $this->getConfig()->get( 'APICacheHelpTimeout' ); - if ( $cacheHelpTimeout > 0 ) { - $cached = $wgMemc->get( $key ); - if ( $cached ) { - return $cached; - } - } - $retval = $this->reallyMakeHelpMsg(); - if ( $cacheHelpTimeout > 0 ) { - $wgMemc->set( $key, $retval, $cacheHelpTimeout ); - } - return $retval; + return ObjectCache::getMainWANInstance()->getWithSetCallback( + wfMemcKey( + 'apihelp', + $this->getModuleName(), + str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) ) + ), + $cacheHelpTimeout > 0 ? $cacheHelpTimeout : WANObjectCache::TTL_UNCACHEABLE, + array( $this, 'reallyMakeHelpMsg' ) + ); } /** diff --git a/includes/cache/BacklinkCache.php b/includes/cache/BacklinkCache.php index 549ac84018..3ba4f6140f 100644 --- a/includes/cache/BacklinkCache.php +++ b/includes/cache/BacklinkCache.php @@ -323,8 +323,9 @@ class BacklinkCache { * @return int */ public function getNumLinks( $table, $max = INF ) { - global $wgMemc, $wgUpdateRowsPerJob; + global $wgUpdateRowsPerJob; + $cache = ObjectCache::getMainWANInstance(); // 1) try partition cache ... if ( isset( $this->partitionCache[$table] ) ) { $entry = reset( $this->partitionCache[$table] ); @@ -340,7 +341,7 @@ class BacklinkCache { $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table ); // 3) ... fallback to memcached ... - $count = $wgMemc->get( $memcKey ); + $count = $cache->get( $memcKey ); if ( $count ) { return min( $max, $count ); } @@ -355,7 +356,7 @@ class BacklinkCache { // Fetch the full title info, since the caller will likely need it next $count = $this->getLinks( $table, false, false, $max )->count(); if ( $count < $max ) { // full count - $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY ); + $cache->set( $memcKey, $count, self::CACHE_EXPIRY ); } } @@ -372,8 +373,6 @@ class BacklinkCache { * @return array */ public function partition( $table, $batchSize ) { - global $wgMemc; - // 1) try partition cache ... if ( isset( $this->partitionCache[$table][$batchSize] ) ) { wfDebug( __METHOD__ . ": got from partition cache\n" ); @@ -381,6 +380,7 @@ class BacklinkCache { return $this->partitionCache[$table][$batchSize]['batches']; } + $cache = ObjectCache::getMainWANInstance(); $this->partitionCache[$table][$batchSize] = false; $cacheEntry =& $this->partitionCache[$table][$batchSize]; @@ -400,7 +400,7 @@ class BacklinkCache { ); // 3) ... fallback to memcached ... - $memcValue = $wgMemc->get( $memcKey ); + $memcValue = $cache->get( $memcKey ); if ( is_array( $memcValue ) ) { $cacheEntry = $memcValue; wfDebug( __METHOD__ . ": got from memcached $memcKey\n" ); @@ -432,11 +432,11 @@ class BacklinkCache { } // Save partitions to memcached - $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY ); + $cache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY ); // Save backlink count to memcached $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table ); - $wgMemc->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY ); + $cache->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY ); wfDebug( __METHOD__ . ": got from database\n" ); diff --git a/includes/diff/DifferenceEngine.php b/includes/diff/DifferenceEngine.php index 654407862e..ae610fad53 100644 --- a/includes/diff/DifferenceEngine.php +++ b/includes/diff/DifferenceEngine.php @@ -680,7 +680,6 @@ class DifferenceEngine extends ContextSource { * @return mixed (string/false) */ public function getDiffBody() { - global $wgMemc; $this->mCacheHit = true; // Check if the diff should be hidden from this user if ( !$this->loadRevisionData() ) { @@ -702,12 +701,13 @@ class DifferenceEngine extends ContextSource { } // Cacheable? $key = false; + $cache = ObjectCache::getMainWANInstance(); if ( $this->mOldid && $this->mNewid ) { $key = $this->getDiffBodyCacheKey(); // Try cache if ( !$this->mRefreshCache ) { - $difftext = $wgMemc->get( $key ); + $difftext = $cache->get( $key ); if ( $difftext ) { wfIncrStats( 'diff_cache.hit' ); $difftext = $this->localiseLineNumbers( $difftext ); @@ -731,7 +731,7 @@ class DifferenceEngine extends ContextSource { wfIncrStats( 'diff_cache.uncacheable' ); } elseif ( $key !== false && $difftext !== false ) { wfIncrStats( 'diff_cache.miss' ); - $wgMemc->set( $key, $difftext, 7 * 86400 ); + $cache->set( $key, $difftext, 7 * 86400 ); } else { wfIncrStats( 'diff_cache.uncacheable' ); }