Add in-process caching to User::idFromName
authorAndrew Garrett <werdna@users.mediawiki.org>
Fri, 26 Jun 2009 14:28:25 +0000 (14:28 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Fri, 26 Jun 2009 14:28:25 +0000 (14:28 +0000)
includes/User.php

index 5849ec0..689c580 100644 (file)
@@ -456,14 +456,29 @@ class User {
                        # Illegal name
                        return null;
                }
+               
+               static $cache = array();
+               
+               if ( isset($cache[$name]) ) {
+                       return $cache[$name];
+               }
+               
                $dbr = wfGetDB( DB_SLAVE );
                $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), __METHOD__ );
 
                if ( $s === false ) {
-                       return 0;
+                       $result = 0;
                } else {
-                       return $s->user_id;
+                       $result = $s->user_id;
                }
+               
+               $cache[$name] = $result;
+               
+               if ( count($cache) > 1000 ) {
+                       $cache = array();
+               }
+               
+               return $result;
        }
 
        /**