Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/250599
Change subject: User: Migrate from foreign cache to global cache for
UserRightsProxy
......................................................................
User: Migrate from foreign cache to global cache for UserRightsProxy
Avoid having one wiki access another wiki's local keyspace.
Instead, use the global keyspace to share values across wikis.
Also, imitating wfMemcKey from wfForeignMemcKey was semantically
incorrect due to $wgCachePrefix having precedence. Most interfaces
(e.g. UserRightsProxy, FileRepo, JobQueue etc.) only have access
to the wiki id (dbname + prefix). The local cache configuration
for wgCachePrefix is not and shouldn't have to be exposed.
Start enforcing that local cache keys are left private and
to share keys, one must use global keys.
Global keys (prefixed with "global:") have their own space and we
can use the wiki-id as regular key segment for keys about users.
Also:
* Expose a method to keep formatting of this key in one place.
As it used used in many different places in core, as well
as in CentralAuth.
* Make use of wfWikiId() in getDefaultKeyspace() to avoid
duplicating this logic.
Change-Id: I58836a24b9e239f460ab489bd2fe8ced8259833c
---
M includes/User.php
M includes/UserRightsProxy.php
M includes/objectcache/ObjectCache.php
3 files changed, 37 insertions(+), 21 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/99/250599/1
diff --git a/includes/User.php b/includes/User.php
index 1727a4a..b59879b 100644
--- a/includes/User.php
+++ b/includes/User.php
@@ -390,6 +390,25 @@
}
/**
+ * @since 1.27
+ * @param string $wikiId
+ * @param integer $userId
+ */
+ public static function purge( $wikiId, $userId ) {
+ $cache = ObjectCache::getMainWANInstance();
+ $cache->delete( $cache->makeGlobalKey( 'user', 'id', $wikiId,
$userId ) );
+ }
+
+ /**
+ * @since 1.27
+ * @param WANObjectCache $cache
+ * @return string
+ */
+ protected function getCacheKey( WANObjectCache $cache ) {
+ return $cache->makeGlobalKey( 'user', 'id', wfWikiID(),
$this->mId );
+ }
+
+ /**
* Load user data from shared cache, given mId has already been set.
*
* @return bool false if the ID does not exist or data is invalid, true
otherwise
@@ -401,8 +420,8 @@
return false;
}
- $key = wfMemcKey( 'user', 'id', $this->mId );
- $data = ObjectCache::getMainWANInstance()->get( $key );
+ $cache = ObjectCache::getMainWANInstance();
+ $data = $cache->get( $this->getCacheKey( $cache ) );
if ( !is_array( $data ) || $data['mVersion'] < self::VERSION ) {
// Object is expired
return false;
@@ -438,10 +457,11 @@
$data[$name] = $this->$name;
}
$data['mVersion'] = self::VERSION;
- $key = wfMemcKey( 'user', 'id', $this->mId );
-
$opts = Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
- ObjectCache::getMainWANInstance()->set( $key, $data, 3600,
$opts );
+
+ $cache = ObjectCache::getMainWANInstance();
+ $key = $this->getCacheKey( $cache );
+ $cache->set( $key, $data, $cache::TTL_HOUR, $opts );
}
/** @name newFrom*() static factory methods */
@@ -2229,17 +2249,17 @@
* @param string $mode Use 'refresh' to clear now; otherwise before DB
commit
*/
public function clearSharedCache( $mode = 'changed' ) {
- $id = $this->getId();
- if ( !$id ) {
+ if ( !$this->getId() ) {
return;
}
- $key = wfMemcKey( 'user', 'id', $id );
+ $cache = ObjectCache::getMainWANInstance();
+ $key = $this->getCacheKey( $cache );
if ( $mode === 'refresh' ) {
- ObjectCache::getMainWANInstance()->delete( $key, 1 );
+ $cache->delete( $key, 1 );
} else {
- wfGetDB( DB_MASTER )->onTransactionPreCommitOrIdle(
function() use ( $key ) {
- ObjectCache::getMainWANInstance()->delete( $key
);
+ wfGetDB( DB_MASTER )->onTransactionPreCommitOrIdle(
function() use ( $cache, $key ) {
+ $cache->delete( $key );
} );
}
}
diff --git a/includes/UserRightsProxy.php b/includes/UserRightsProxy.php
index 3a3eb53..e686ae3 100644
--- a/includes/UserRightsProxy.php
+++ b/includes/UserRightsProxy.php
@@ -278,9 +278,10 @@
array( 'user_id' => $this->id ),
__METHOD__ );
- $key = wfForeignMemcKey( $this->database, false, 'user', 'id',
$this->id );
- $this->db->onTransactionPreCommitOrIdle( function() use ( $key
) {
- ObjectCache::getMainWANInstance()->delete( $key );
+ $wikiId = $this->db->getWikiID();
+ $userId = $this->id;
+ $this->db->onTransactionPreCommitOrIdle( function() use (
$wikiId, $userId ) {
+ User::purge( $wikiId, $userId );
} );
}
}
diff --git a/includes/objectcache/ObjectCache.php
b/includes/objectcache/ObjectCache.php
index 3d14c33..abb88ba 100644
--- a/includes/objectcache/ObjectCache.php
+++ b/includes/objectcache/ObjectCache.php
@@ -137,19 +137,14 @@
* @return string
*/
public static function getDefaultKeyspace() {
- global $wgCachePrefix, $wgDBname, $wgDBprefix;
+ global $wgCachePrefix;
$keyspace = $wgCachePrefix;
if ( is_string( $keyspace ) && $keyspace !== '' ) {
return $keyspace;
}
- $keyspace = $wgDBname;
- if ( is_string( $wgDBprefix ) && $wgDBprefix !== '' ) {
- $keyspace .= '-' . $wgDBprefix;
- }
-
- return $keyspace;
+ return wfWikiID();
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/250599
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I58836a24b9e239f460ab489bd2fe8ced8259833c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: wmf/1.27.0-wmf.4
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits