Merge "Prevent duplicate memcached lookups for user record"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 26 Feb 2016 18:38:09 +0000 (18:38 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 26 Feb 2016 18:38:09 +0000 (18:38 +0000)
1  2 
includes/user/User.php

diff --combined includes/user/User.php
@@@ -315,12 -315,9 +315,12 @@@ class User implements IDBAccessObject 
        }
  
        /**
 -       * Test if it's safe to load this User object. You should typically check this before using
 -       * $wgUser or RequestContext::getUser in a method that might be called before the system has
 -       * been fully initialized. If the object is unsafe, you should use an anonymous user:
 +       * Test if it's safe to load this User object.
 +       *
 +       * You should typically check this before using $wgUser or
 +       * RequestContext::getUser in a method that might be called before the
 +       * system has been fully initialized. If the object is unsafe, you should
 +       * use an anonymous user:
         * \code
         * $user = $wgUser->isSafeToLoad() ? $wgUser : new User;
         * \endcode
         */
        public function isSafeToLoad() {
                global $wgFullyInitialised;
 -              return $wgFullyInitialised || $this->mLoadedItems === true || $this->mFrom !== 'session';
 +
 +              // The user is safe to load if:
 +              // * MW_NO_SESSION is undefined AND $wgFullyInitialised is true (safe to use session data)
 +              // * mLoadedItems === true (already loaded)
 +              // * mFrom !== 'session' (sessions not involved at all)
 +
 +              return ( !defined( 'MW_NO_SESSION' ) && $wgFullyInitialised ) ||
 +                      $this->mLoadedItems === true || $this->mFrom !== 'session';
        }
  
        /**
                }
  
                $cache = ObjectCache::getMainWANInstance();
-               $data = $cache->get( $this->getCacheKey( $cache ) );
-               if ( !is_array( $data ) || $data['mVersion'] < self::VERSION ) {
-                       // Object is expired
-                       return false;
+               $key = $this->getCacheKey( $cache );
+               $processCache = ObjectCache::getLocalServerInstance( 'hash' );
+               $data = $processCache->get( $key );
+               if ( !is_array( $data ) ) {
+                       $data = $cache->get( $key );
+                       if ( !is_array( $data ) || $data['mVersion'] < self::VERSION ) {
+                               // Object is expired
+                               return false;
+                       }
+                       $processCache->set( $key, $data );
                }
                wfDebug( "User: got user {$this->mId} from cache\n" );
  
                // Restore from cache