r52460 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52459‎ | r52460 | r52461 >
Date:14:28, 26 June 2009
Author:werdna
Status:ok (Comments)
Tags:todo 
Comment:
Add in-process caching to User::idFromName
Modified paths:
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -456,14 +456,29 @@
457457 # Illegal name
458458 return null;
459459 }
 460+
 461+ static $cache = array();
 462+
 463+ if ( isset($cache[$name]) ) {
 464+ return $cache[$name];
 465+ }
 466+
460467 $dbr = wfGetDB( DB_SLAVE );
461468 $s = $dbr->selectRow( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), __METHOD__ );
462469
463470 if ( $s === false ) {
464 - return 0;
 471+ $result = 0;
465472 } else {
466 - return $s->user_id;
 473+ $result = $s->user_id;
467474 }
 475+
 476+ $cache[$name] = $result;
 477+
 478+ if ( count($cache) > 1000 ) {
 479+ $cache = array();
 480+ }
 481+
 482+ return $result;
468483 }
469484
470485 /**

Comments

#Comment by Brion VIBBER (talk | contribs)   00:22, 24 August 2009

What if a user is renamed during the script run?

#Comment by Werdna (talk | contribs)   19:01, 27 August 2009

Marking as TODO, doesn't block deployment.

#Comment by Brion VIBBER (talk | contribs)   16:09, 20 September 2011

Hmm, why didn't this block deployment? Long-running scripts do, in fact, exist and are used in production. :) Bug 31030 cites the in-process cache in this revision as a potential data-corrupter.

Status & tagging log