Skip to content

Commit

Permalink
Merge pull request #412 from nextcloud/perf/cache-user-when-checking-…
Browse files Browse the repository at this point in the history
…existance

Cache user object when checking existance
  • Loading branch information
juliusknorr authored Apr 5, 2022
2 parents 14379d4 + 51793f2 commit b2df41b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/Db/UserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;
use OC\Cache\CappedMemoryCache;

class UserMapper extends QBMapper {
/** @var ProviderService */
private $providerService;
/** CappedMemoryCache */
private $userCache;

public function __construct(IDBConnection $db, ProviderService $providerService) {
parent::__construct($db, 'user_oidc', User::class);
$this->providerService = $providerService;
$this->userCache = new CappedMemoryCache();
}

/**
Expand All @@ -46,6 +50,9 @@ public function __construct(IDBConnection $db, ProviderService $providerService)
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
*/
public function getUser(string $uid): User {
if ($this->userCache->hasKey($uid)) {
return $this->userCache->get($uid);
}
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand Down Expand Up @@ -93,7 +100,7 @@ public function findDisplayNames(string $search, $limit = null, $offset = null):

public function userExists(string $uid): bool {
try {
$this->getUser($uid);
$this->userCache->set($uid, $this->getUser($uid));
return true;
} catch (IMapperException $e) {
return false;
Expand Down

0 comments on commit b2df41b

Please sign in to comment.