Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

SUS-1620 | Introduced User::touch() method to bump the getTouched() value using memcached #12923

Merged
merged 3 commits into from
May 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 54 additions & 41 deletions includes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,18 @@ class User implements JsonSerializable {
/** @name Cache variables */
//@{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this commented out code. rule

var $mId, $mName, $mRealName,
$mEmail, $mTouched, $mToken, $mEmailAuthenticated,
$mEmail, $mToken, $mEmailAuthenticated,
$mEmailToken, $mEmailTokenExpires, $mRegistration, $mGroups, $mOptionOverrides,
$mCookiePassword, $mEditCount, $mAllowUsertalk;
var $mBirthDate; // Wikia. Added to reflect our user table layout.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INFO Move this trailing comment on the previous empty line. rule

//@}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this commented out code. rule


/** @var string TS_MW timestamp from the DB */
public $mTouched;

/** @var string TS_MW timestamp from cache */
protected $mQuickTouched;

/**
* Bool Whether the cache variables have been loaded.
*/
Expand Down Expand Up @@ -2001,51 +2007,42 @@ private function clearSharedCache() {

/**
* Immediately touch the user data cache for this account.
* Updates user_touched field, and removes account data from memcached
* for reload on the next hit.
*
* Calls touch() and removes account data from memcached
*
* @see SUS-1620
*/
public function invalidateCache() {
#<Wikia>
global $wgLogUserInvalidateCache;
if ( !empty( $wgLogUserInvalidateCache ) ) {
$e = new Exception;
$this->error( 'SUS-546', [ 'traceBack' => $e->getTraceAsString() ] );
}
#</Wikia>
if( wfReadOnly() ) {
return;
}
$this->load();
if ( wfReadOnly() ) {
return;
}
if( $this->mId ) {
$this->mTouched = self::newTouchedTimestamp();
$this->touch();
$this->clearSharedCache();

#<Wikia>
global $wgExternalSharedDB, $wgSharedDB;
if( isset( $wgSharedDB ) ) {
$dbw = wfGetDB( DB_MASTER, array(), $wgExternalSharedDB );
}
else {
$dbw = wfGetDB( DB_MASTER );
}
#</Wikia>
// Wikia change
self::permissionsService()->invalidateCache( $this );
}

$touched = $dbw->timestamp( $this->mTouched );
$needsPurge = $dbw->selectField(
'`user`', '1',
array( 'user_id' => $this->mId, 'user_touched < ' . $dbw->addQuotes( $touched ) ),
__METHOD__ );
/**
* Update the "touched" timestamp for the user
*
* This is useful on various login/logout events when making sure that
* a browser or proxy that has multiple tenants does not suffer cache
* pollution where the new user sees the old users content. The value
* of getTouched() is checked when determining 304 vs 200 responses.
* Unlike invalidateCache(), this preserves the User object cache and
* avoids database writes.
*
* @@see SUS-1620
* @since 1.25
*/
public function touch() {
global $wgMemc;

if ( $needsPurge ) {
$dbw->update( '`user`',
array( 'user_touched' => $touched ), array( 'user_id' => $this->mId ),
__METHOD__ );
}
self::permissionsService()->invalidateCache( $this );
$this->load();

$this->clearSharedCache();
if ( $this->mId ) {
$key = wfSharedMemcKey( 'user-quicktouched', 'id', $this->mId );
$timestamp = self::newTouchedTimestamp();
$wgMemc->set( $key, $timestamp );
$this->mQuickTouched = $timestamp;
}
}

Expand All @@ -2062,10 +2059,26 @@ public function validateCache( $timestamp ) {

/**
* Get the user touched timestamp
* @return String timestamp
* @return string TS_MW Timestamp
*/
public function getTouched() {
global $wgMemc;

$this->load();

if ( $this->mId ) {
if ( $this->mQuickTouched === null ) {
$key = wfSharedMemcKey( 'user-quicktouched', 'id', $this->mId );
$timestamp = $wgMemc->get( $key );
if ( !$timestamp ) {
# Set the timestamp to get HTTP 304 cache hits
$this->touch();
}
}

return max( $this->mTouched, $this->mQuickTouched );
}

return $this->mTouched;
}

Expand Down