Cache account information

Currently, each field of the profile settings is fetching the account
information. This patch makes it so that only the first time do a DB call
and all the later ones are cached.

Reduce by 5 queries when loading the profile setting page and I suppose
other pages are affected since loading a page generates always fetch at
least once the account information to see if the profile feature is enabled
for the user.

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
pull/31849/head
Carl Schwan 4 years ago
parent 9c84aa5870
commit 9cb992e93c
  1. 11
      lib/private/Accounts/AccountManager.php

@ -41,6 +41,7 @@ use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use OC\Profile\TProfileHelper;
use OC\Cache\CappedMemoryCache;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
@ -116,6 +117,7 @@ class AccountManager implements IAccountManager {
private $crypto;
/** @var IFactory */
private $l10nfactory;
private CappedMemoryCache $internalCache;
public function __construct(
IDBConnection $connection,
@ -142,6 +144,7 @@ class AccountManager implements IAccountManager {
$this->crypto = $crypto;
// DIing IL10N results in a dependency loop
$this->l10nfactory = $factory;
$this->internalCache = new CappedMemoryCache();
}
/**
@ -763,7 +766,12 @@ class AccountManager implements IAccountManager {
}
public function getAccount(IUser $user): IAccount {
return $this->parseAccountData($user, $this->getUser($user));
if ($this->internalCache->hasKey($user->getUID())) {
return $this->internalCache->get($user->getUID());
}
$account = $this->parseAccountData($user, $this->getUser($user));
$this->internalCache->set($user->getUID(), $account);
return $account;
}
public function updateAccount(IAccount $account): void {
@ -813,5 +821,6 @@ class AccountManager implements IAccountManager {
}
$this->updateUser($account->getUser(), $data, true);
$this->internalCache->set($account->getUser()->getUID(), $account);
}
}

Loading…
Cancel
Save