(bug 39490) Add caching to InfoAction.
authorTyler Anthony Romeo <tylerromeo@gmail.com>
Mon, 8 Oct 2012 03:47:46 +0000 (23:47 -0400)
committerTyler Anthony Romeo <tylerromeo@gmail.com>
Tue, 18 Dec 2012 20:33:00 +0000 (15:33 -0500)
Added caching to InfoAction. The cache key uses the ID of the
latest revision so it is auto-invalidated when the page is
changed. Also, the cache key is deleted whenever the page is
purged.

Change-Id: I90446b7bcb4517959605aa38eacfada2b785060b

includes/Title.php
includes/actions/InfoAction.php

index 896218b..bdaa66f 100644 (file)
@@ -4461,6 +4461,7 @@ class Title {
         * @return Bool true if the update succeded
         */
        public function invalidateCache() {
+               global $wgMemc;
                if ( wfReadOnly() ) {
                        return false;
                }
@@ -4472,6 +4473,14 @@ class Title {
                        __METHOD__
                );
                HTMLFileCache::clearFileCache( $this );
+
+               // Clear page info.
+               $revision = WikiPage::factory( $this )->getRevision();
+               if( $revision !== null ) {
+                       $memcKey = wfMemcKey( 'infoaction', $this->getPrefixedText(), $revision->getId() );
+                       $success = $success && $wgMemc->delete( $memcKey );
+               }
+
                return $success;
        }
 
index 29c3d7e..fb8aea6 100644 (file)
@@ -165,15 +165,21 @@ class InfoAction extends FormlessAction {
         * @return array
         */
        protected function pageInfo() {
-               global $wgContLang, $wgRCMaxAge;
+               global $wgContLang, $wgRCMaxAge, $wgMemc;
 
                $user = $this->getUser();
                $lang = $this->getLanguage();
                $title = $this->getTitle();
                $id = $title->getArticleID();
 
-               // Get page information that would be too "expensive" to retrieve by normal means
-               $pageCounts = self::pageCounts( $title, $user );
+               $memcKey = wfMemcKey( 'infoaction', $title->getPrefixedText(), $this->page->getRevision()->getId() );
+               $pageCounts = $wgMemc->get( $memcKey );
+               if ( $pageCounts === false ) {
+                       // Get page information that would be too "expensive" to retrieve by normal means
+                       $pageCounts = self::pageCounts( $title );
+
+                       $wgMemc->set( $memcKey, $pageCounts );
+               }
 
                // Get page properties
                $dbr = wfGetDB( DB_SLAVE );
@@ -259,7 +265,7 @@ class InfoAction extends FormlessAction {
                        );
                }
 
-               if ( isset( $pageCounts['watchers'] ) ) {
+               if ( $user->isAllowed( 'unwatchedpages' ) ) {
                        // Number of page watchers
                        $pageInfo['header-basic'][] = array(
                                $this->msg( 'pageinfo-watchers' ), $lang->formatNum( $pageCounts['watchers'] )
@@ -473,11 +479,10 @@ class InfoAction extends FormlessAction {
        /**
         * Returns page counts that would be too "expensive" to retrieve by normal means.
         *
-        * @param $title Title object
-        * @param $user User object
+        * @param Title $title Title to get counts for
         * @return array
         */
-       protected static function pageCounts( $title, $user ) {
+       protected static function pageCounts( Title $title ) {
                global $wgRCMaxAge, $wgDisableCounters;
 
                wfProfileIn( __METHOD__ );
@@ -497,19 +502,17 @@ class InfoAction extends FormlessAction {
                        $result['views'] = $views;
                }
 
-               if ( $user->isAllowed( 'unwatchedpages' ) ) {
-                       // Number of page watchers
-                       $watchers = (int) $dbr->selectField(
-                               'watchlist',
-                               'COUNT(*)',
-                               array(
-                                       'wl_namespace' => $title->getNamespace(),
-                                       'wl_title'     => $title->getDBkey(),
-                               ),
-                               __METHOD__
-                       );
-                       $result['watchers'] = $watchers;
-               }
+               // Number of page watchers
+               $watchers = (int) $dbr->selectField(
+                       'watchlist',
+                       'COUNT(*)',
+                       array(
+                               'wl_namespace' => $title->getNamespace(),
+                               'wl_title'     => $title->getDBkey(),
+                       ),
+                       __METHOD__
+               );
+               $result['watchers'] = $watchers;
 
                // Total number of edits
                $edits = (int) $dbr->selectField(