[MediaWiki-commits] [Gerrit] mediawiki...CentralAuth[master]: Various fixes to WikiSet caching logic
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/393673 ) Change subject: Various fixes to WikiSet caching logic .. Various fixes to WikiSet caching logic * Make the by-name caching actually work. * Use the proper getGlobalKey() method. This makes the by-id lookup keys have a namespace. * Avoid duplicating the name/id key generation logic. * Use getWithSetCallback() instead of get()/set(). * Remove unused $useCache parameter. Change-Id: Iaaed90d36bc992af43e3739d33e86b723bdcefee --- M includes/WikiSet.php 1 file changed, 103 insertions(+), 67 deletions(-) Approvals: Krinkle: Looks good to me, approved jenkins-bot: Verified Anomie: Looks good to me, but someone else must approve diff --git a/includes/WikiSet.php b/includes/WikiSet.php index ad2fccd..dda1d30 100644 --- a/includes/WikiSet.php +++ b/includes/WikiSet.php @@ -1,5 +1,8 @@ mName = $name; $this->mType = $type; $this->mWikis = $wikis; - } - - /** -* @param string $k -* @return string -*/ - protected static function memcKey( $k ) { - return "wikiset:{$k}"; } /** @@ -93,7 +84,6 @@ /** * @param string $t -* @return bool */ public function setType( $t ) { if ( !in_array( $t, [ self::OPTIN, self::OPTOUT ] ) ) { @@ -130,64 +120,103 @@ /** * @param string $name -* @param bool $useCache * @return null|WikiSet */ - public static function newFromName( $name, $useCache = true ) { - if ( $useCache ) { - $cache = ObjectCache::getMainWANInstance(); - $data = $cache->get( self::memcKey( "name:" . md5( $name ) ) ); - if ( $data ) { - if ( $data['mVersion'] == self::VERSION ) { - $ws = new WikiSet( null, null ); - foreach ( $data as $key => $val ) { - $ws->$key = $val; - } - return $ws; + public static function newFromName( $name ) { + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + + $data = $cache->getWithSetCallback( + self::getPerNameCacheKey( $cache, $name ), + $cache::TTL_INDEFINITE, + function ( $oldValue, &$ttl, &$setOpts ) use ( $name ) { + $dbr = CentralAuthUtils::getCentralSlaveDB(); + $setOpts += Database::getCacheSetOptions( $dbr ); + + $row = $dbr->selectRow( 'wikiset', '*', [ 'ws_name' => $name ], __METHOD__ ); + + $wikiSet = self::newFromRow( $row ); + if ( $wikiSet ) { + $value = $wikiSet->getDataForCache(); + } else { + $ttl = WANObjectCache::TTL_MINUTE; // cache negatives + $value = null; } - } - } - $dbr = CentralAuthUtils::getCentralSlaveDB(); - $row = $dbr->selectRow( - 'wikiset', '*', [ 'ws_name' => $name ], __METHOD__ + + return $value; + }, + [ 'version' => self::VERSION ] ); - if ( !$row ) { + + if ( !$data ) { return null; } - $ws = self::newFromRow( $row ); - $ws->saveToCache(); - return $ws; + + $wikiSet = new WikiSet( null, null ); + $wikiSet->loadFromCachedData( $data ); + + return $wikiSet; } /** * @param string|int $id -* @param bool $useCache * @return null|WikiSet */ - public static function newFromID( $id, $useCache = true ) { - if ( $useCache ) { - $cache = ObjectCache::getMainWANInstance(); - $data = $cache->get( self::memcKey( $id ) ); - if ( $data ) { - if ( $data['mVersion'] == self::VERSION ) { - $ws = new WikiSet( null, null ); - foreach ( $data as $name => $val ) { - $ws->$name = $val; - } - return $ws; + public static function newFromID( $id ) { +
[MediaWiki-commits] [Gerrit] mediawiki...CentralAuth[master]: Various fixes to WikiSet caching logic
Aaron Schulz has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/393673 ) Change subject: Various fixes to WikiSet caching logic .. Various fixes to WikiSet caching logic * Make the by-name caching actually work. * Use the proper getGlobalKey() method. This makes the by-id lookup keys have a namespace. * Avoid duplicating the name/id key generation logic. * Use getWithSetCallback() instead of get()/set(). * Remove unused $useCache parameter. Change-Id: Iaaed90d36bc992af43e3739d33e86b723bdcefee --- M includes/WikiSet.php 1 file changed, 104 insertions(+), 68 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralAuth refs/changes/73/393673/1 diff --git a/includes/WikiSet.php b/includes/WikiSet.php index ad2fccd..48ec121 100644 --- a/includes/WikiSet.php +++ b/includes/WikiSet.php @@ -1,5 +1,8 @@ mName = $name; $this->mType = $type; $this->mWikis = $wikis; - } - - /** -* @param string $k -* @return string -*/ - protected static function memcKey( $k ) { - return "wikiset:{$k}"; } /** @@ -93,7 +84,6 @@ /** * @param string $t -* @return bool */ public function setType( $t ) { if ( !in_array( $t, [ self::OPTIN, self::OPTOUT ] ) ) { @@ -130,64 +120,103 @@ /** * @param string $name -* @param bool $useCache * @return null|WikiSet */ - public static function newFromName( $name, $useCache = true ) { - if ( $useCache ) { - $cache = ObjectCache::getMainWANInstance(); - $data = $cache->get( self::memcKey( "name:" . md5( $name ) ) ); - if ( $data ) { - if ( $data['mVersion'] == self::VERSION ) { - $ws = new WikiSet( null, null ); - foreach ( $data as $key => $val ) { - $ws->$key = $val; - } - return $ws; + public static function newFromName( $name ) { + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + + $data = $cache->getWithSetCallback( + self::getKeyByNameCacheKey( $cache, $name ), + $cache::TTL_INDEFINITE, + function ( $oldValue, &$ttl, &$setOpts ) use ( $name ) { + $dbr = CentralAuthUtils::getCentralSlaveDB(); + $setOpts += Database::getCacheSetOptions( $dbr ); + + $row = $dbr->selectRow( 'wikiset', '*', [ 'ws_name' => $name ], __METHOD__ ); + + $wikiSet = self::newFromRow( $row ); + if ( $wikiSet ) { + $value = $wikiSet->getDataForCache(); + } else { + $ttl = WANObjectCache::TTL_MINUTE; // cache negatives + $value = null; } - } - } - $dbr = CentralAuthUtils::getCentralSlaveDB(); - $row = $dbr->selectRow( - 'wikiset', '*', [ 'ws_name' => $name ], __METHOD__ + + return $value; + }, + [ 'version' => self::VERSION ] ); - if ( !$row ) { - return null; + + if ( $data ) { + $wikiSet = new WikiSet( null, null ); + $wikiSet->loadFromCachedData( $data ); + + return $wikiSet; } - $ws = self::newFromRow( $row ); - $ws->saveToCache(); - return $ws; + + return null; } /** * @param string|int $id -* @param bool $useCache * @return null|WikiSet */ - public static function newFromID( $id, $useCache = true ) { - if ( $useCache ) { - $cache = ObjectCache::getMainWANInstance(); - $data = $cache->get( self::memcKey( $id ) ); - if ( $data ) { - if ( $data['mVersion'] == self::VERSION ) { - $ws = new WikiSet( null, null ); - foreach ( $data as $name => $val ) { - $ws->$name = $val; - } - return $ws; + public static function newFrom