mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/OATHAuth
synced 2024-11-28 02:00:06 +00:00
Add in-process cache for OATHUser lookups
Change-Id: I9392b7b1a23944dfd91d690fa30b5a7fdf0f2e51
This commit is contained in:
parent
ac04c1fd9f
commit
d5ec363ce6
|
@ -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;
|
||||
|
|
|
@ -13,6 +13,7 @@ class OATHUser {
|
|||
private $key;
|
||||
|
||||
/**
|
||||
* Constructor. Can't be called directly. Use OATHUserRepository::findByUser instead.
|
||||
* @param User $user
|
||||
* @param OATHAuthKey $key
|
||||
*/
|
||||
|
|
|
@ -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() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue