Convert some users to WANObjectCache for consistency
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 15 Oct 2015 02:45:03 +0000 (19:45 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 19 Oct 2015 16:46:52 +0000 (16:46 +0000)
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

includes/AjaxResponse.php
includes/Block.php
includes/Revision.php
includes/SiteStats.php
includes/api/ApiHelp.php
includes/api/ApiMain.php
includes/cache/BacklinkCache.php
includes/diff/DifferenceEngine.php

index 6c2efc2..34bb65f 100644 (file)
@@ -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
index 0ec4ad1..5dee23e 100644 (file)
@@ -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" );
 
index 24c025f..a498817 100644 (file)
@@ -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;
index 76e7f7e..b84d2bf 100644 (file)
@@ -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 )
+               );
        }
 
        /**
index abec828..c33b27b 100644 (file)
@@ -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 );
                }
        }
 
index 5d2db47..c641c95 100644 (file)
@@ -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' )
+               );
        }
 
        /**
index 549ac84..3ba4f61 100644 (file)
@@ -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" );
 
index 6544078..ae610fa 100644 (file)
@@ -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' );
                }