From 12b76fcabbdeb3be937c5ed4af8eb334d0d91317 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 27 Nov 2012 09:26:02 -0800 Subject: [PATCH] Made BackLinkCache use object caching better. * This way, we can actually get persistent cache hits for the HTMLCacheUpdate queries rather than always hitting a DB. * Also moved BacklinkCache under the /cache directory. Change-Id: I0666ee575fb42675f1a7dd9cb52665f0a12a66a9 --- includes/AutoLoader.php | 2 +- includes/{ => cache}/BacklinkCache.php | 42 ++++++++++++++++---------- 2 files changed, 27 insertions(+), 17 deletions(-) rename includes/{ => cache}/BacklinkCache.php (94%) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 9bdf3778cd..31c1571987 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -38,7 +38,6 @@ $wgAutoloadLocalClasses = array( 'AuthPlugin' => 'includes/AuthPlugin.php', 'AuthPluginUser' => 'includes/AuthPlugin.php', 'Autopromote' => 'includes/Autopromote.php', - 'BacklinkCache' => 'includes/BacklinkCache.php', 'BadTitleError' => 'includes/Exception.php', 'BaseTemplate' => 'includes/SkinTemplate.php', 'Block' => 'includes/Block.php', @@ -425,6 +424,7 @@ $wgAutoloadLocalClasses = array( 'UsageException' => 'includes/api/ApiMain.php', # includes/cache + 'BacklinkCache' => 'includes/cache/BacklinkCache.php', 'CacheDependency' => 'includes/cache/CacheDependency.php', 'ConstantDependency' => 'includes/cache/CacheDependency.php', 'DependencyWrapper' => 'includes/cache/CacheDependency.php', diff --git a/includes/BacklinkCache.php b/includes/cache/BacklinkCache.php similarity index 94% rename from includes/BacklinkCache.php rename to includes/cache/BacklinkCache.php index ba8691b292..c4f8762a24 100644 --- a/includes/BacklinkCache.php +++ b/includes/cache/BacklinkCache.php @@ -303,18 +303,32 @@ class BacklinkCache { * @return integer */ public function getNumLinks( $table ) { - if ( isset( $this->fullResultCache[$table] ) ) { - return $this->fullResultCache[$table]->numRows(); - } + global $wgMemc; + // 1) try partition cache ... if ( isset( $this->partitionCache[$table] ) ) { $entry = reset( $this->partitionCache[$table] ); return $entry['numRows']; } - $titleArray = $this->getLinks( $table ); + // 2) ... then try full result cache ... + if ( isset( $this->fullResultCache[$table] ) ) { + return $this->fullResultCache[$table]->numRows(); + } + + $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table ); + + // 3) ... fallback to memcached ... + $count = $wgMemc->get( $memcKey ); + if ( $count ) { + return $count; + } + + // 4) fetch from the database ... + $count = $this->getLinks( $table )->count(); + $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY ); - return $titleArray->count(); + return $count; } /** @@ -327,9 +341,9 @@ 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" ); return $this->partitionCache[$table][$batchSize]['batches']; @@ -339,18 +353,12 @@ class BacklinkCache { $cacheEntry =& $this->partitionCache[$table][$batchSize]; // 2) ... then try full result cache ... - if ( isset( $this->fullResultCache[$table] ) ) { $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize ); wfDebug( __METHOD__ . ": got from full result cache\n" ); - return $cacheEntry['batches']; } - // 3) ... fallback to memcached ... - - global $wgMemc; - $memcKey = wfMemcKey( 'backlinks', md5( $this->title->getPrefixedDBkey() ), @@ -358,23 +366,25 @@ class BacklinkCache { $batchSize ); + // 3) ... fallback to memcached ... $memcValue = $wgMemc->get( $memcKey ); - if ( is_array( $memcValue ) ) { $cacheEntry = $memcValue; wfDebug( __METHOD__ . ": got from memcached $memcKey\n" ); - return $cacheEntry['batches']; } // 4) ... finally fetch from the slow database :( - $this->getLinks( $table ); $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize ); - // Save to memcached + // Save partitions to memcached $wgMemc->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 ); + wfDebug( __METHOD__ . ": got from database\n" ); return $cacheEntry['batches']; } -- 2.20.1