jenkins-bot has submitted this change and it was merged. Change subject: Cache user data in memory ......................................................................
Cache user data in memory Stores user data looked up in WAN cache in memory so that lookups in the same request do not result in new memcached lookups. Quick and dirty solution, but nicer ones are more difficult: * no idea what replacing WANObjectCache::get/set with getWithSetCallback (which has its own in-process cache) would do, the code is complex and completely different * would be nice to wrap the logic into a proxy object (like CachedBagOStuff) but WANObjectCache calls set() internally (and marks it final), so inheriting is not safe; the interface and implementation should be separated, and that means updating all external callers which do a type check. * ObjectCache::getInstance('hash') cannot be used because it has no item limit and this could eat up the memory with a script that iterates through lots of users The patch does not attempt to replicate tombstoning for User::clearSharedCache('refresh'). Based on Iec1504700a and Idef9a9d3. Change-Id: I419f356b0c306d16711b433da95dccdb44645154 Co-Authored-By: Ori Livneh <o...@wikimedia.org> Bug: T128157 (cherry picked from commit 9c733318107e26644fec33430e9712e51b022ad2) --- M includes/user/User.php 1 file changed, 41 insertions(+), 9 deletions(-) Approvals: Ori.livneh: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/user/User.php b/includes/user/User.php index 7caa3f9..751275d 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -187,6 +187,12 @@ */ protected static $mAllRights = false; + /** + * An in-process cache for user data lookup + * @var HashBagOStuff + */ + protected static $inProcessCache; + /** Cache variables */ // @{ public $mId; @@ -433,7 +439,10 @@ */ public static function purge( $wikiId, $userId ) { $cache = ObjectCache::getMainWANInstance(); - $cache->delete( $cache->makeGlobalKey( 'user', 'id', $wikiId, $userId ) ); + $processCache = self::getInProcessCache(); + $key = $cache->makeGlobalKey( 'user', 'id', $wikiId, $userId ); + $cache->delete( $key ); + $processCache->delete( $key ); } /** @@ -443,6 +452,17 @@ */ protected function getCacheKey( WANObjectCache $cache ) { return $cache->makeGlobalKey( 'user', 'id', wfWikiID(), $this->mId ); + } + + /** + * @since 1.27 + * @return HashBagOStuff + */ + protected static function getInProcessCache() { + if ( !self::$inProcessCache ) { + self::$inProcessCache = new HashBagOStuff( ['maxKeys' => 10] ); + } + return self::$inProcessCache; } /** @@ -458,12 +478,17 @@ } $cache = ObjectCache::getMainWANInstance(); - $data = $cache->get( $this->getCacheKey( $cache ) ); - if ( !is_array( $data ) || $data['mVersion'] < self::VERSION ) { - // Object is expired - return false; + $processCache = self::getInProcessCache(); + $key = $this->getCacheKey( $cache ); + $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 @@ -497,8 +522,10 @@ $opts = Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) ); $cache = ObjectCache::getMainWANInstance(); + $processCache = self::getInProcessCache(); $key = $this->getCacheKey( $cache ); $cache->set( $key, $data, $cache::TTL_HOUR, $opts ); + $processCache->set( $key, $data ); } /** @name newFrom*() static factory methods */ @@ -2286,13 +2313,18 @@ } $cache = ObjectCache::getMainWANInstance(); + $processCache = self::getInProcessCache(); $key = $this->getCacheKey( $cache ); if ( $mode === 'refresh' ) { $cache->delete( $key, 1 ); + $processCache->delete( $key ); } else { - wfGetDB( DB_MASTER )->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) { - $cache->delete( $key ); - } ); + wfGetDB( DB_MASTER )->onTransactionPreCommitOrIdle( + function() use ( $cache, $processCache, $key ) { + $cache->delete( $key ); + $processCache->delete( $key ); + } + ); } } -- To view, visit https://gerrit.wikimedia.org/r/274040 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I419f356b0c306d16711b433da95dccdb44645154 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: wmf/1.27.0-wmf.14 Gerrit-Owner: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: Gergő Tisza <gti...@wikimedia.org> Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits