Merge "Add in-process cache for OATHUser lookups"

This commit is contained in:
jenkins-bot 2017-08-07 21:11:19 +00:00 committed by Gerrit Code Review
commit eea5cc028f
3 changed files with 33 additions and 10 deletions

View file

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

View file

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

View file

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