mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/OATHAuth
synced 2024-11-13 18:16:56 +00:00
Merge "Add in-process cache for OATHUser lookups"
This commit is contained in:
commit
eea5cc028f
|
@ -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;
|
||||||
|
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue