Prevent duplicate memcached lookups for user record
authorOri Livneh <ori@wikimedia.org>
Fri, 26 Feb 2016 04:49:41 +0000 (20:49 -0800)
committerOri Livneh <ori@wikimedia.org>
Fri, 26 Feb 2016 07:50:19 +0000 (23:50 -0800)
User::loadFromId() will look up the user in the database or in memcached -- but
it does not allow for the possibility that the record has already been
retrieved. On a typical page request on the Wikimedia cluster, this causes over
a dozen duplicate memcached lookups for the user record.

Bug: T128157
Change-Id: Iec1504700ab566ca89d0ef868d495238b151034a

includes/user/User.php

index eb3853a..90c05d8 100644 (file)
@@ -458,12 +458,18 @@ class User implements IDBAccessObject {
                }
 
                $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