Add in-process cache for OATHUser lookups

Change-Id: I9392b7b1a23944dfd91d690fa30b5a7fdf0f2e51
This commit is contained in:
Gergő Tisza 2016-06-15 10:07:01 +00:00 committed by Gergő Tisza
parent ac04c1fd9f
commit d5ec363ce6
3 changed files with 33 additions and 10 deletions

View file

@ -22,7 +22,14 @@ class OATHAuthHooks {
if ( $service == null ) {
$factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$service = new OATHUserRepository( $factory->getMainLB( $wgOATHAuthDatabase ) );
$service = new OATHUserRepository(
$factory->getMainLB( $wgOATHAuthDatabase ),
new HashBagOStuff(
[
'maxKeys' => 5,
]
)
);
}
return $service;

View file

@ -13,6 +13,7 @@ class OATHUser {
private $key;
/**
* Constructor. Can't be called directly. Use OATHUserRepository::findByUser instead.
* @param User $user
* @param OATHAuthKey $key
*/

View file

@ -4,12 +4,17 @@ class OATHUserRepository {
/** @var LoadBalancer */
protected $lb;
/** @var BagOStuff */
protected $cache;
/**
* OATHUserRepository constructor.
* @param LoadBalancer $lb
* @param BagOStuff $cache
*/
public function __construct( LoadBalancer $lb ) {
public function __construct( LoadBalancer $lb, BagOStuff $cache ) {
$this->lb = $lb;
$this->cache = $cache;
}
/**
@ -17,16 +22,24 @@ class OATHUserRepository {
* @return OATHUser
*/
public function findByUser( User $user ) {
$oathUser = new OATHUser( $user, null );
$oathUser = $this->cache->get( $user->getName() );
if ( !$oathUser ) {
$oathUser = new OATHUser( $user, null );
$uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
$res = $this->getDB( DB_SLAVE )
->selectRow( 'oathauth_users', '*', [ 'id' => $uid ], __METHOD__ );
if ( $res ) {
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
$oathUser->setKey( $key );
$uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
$res = $this->getDB( DB_SLAVE )->selectRow(
'oathauth_users',
'*',
[ 'id' => $uid ],
__METHOD__
);
if ( $res ) {
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
$oathUser->setKey( $key );
}
$this->cache->set( $user->getName(), $oathUser );
}
return $oathUser;
}
@ -44,6 +57,7 @@ class OATHUserRepository {
],
__METHOD__
);
$this->cache->set( $user->getUser()->getName(), $user );
}
/**
@ -55,6 +69,7 @@ class OATHUserRepository {
[ 'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ) ],
__METHOD__
);
$this->cache->delete( $user->getUser()->getName() );
}
/**