Class AccountCacheImpl

java.lang.Object
com.google.gerrit.server.account.AccountCacheImpl
All Implemented Interfaces:
AccountCache

public class AccountCacheImpl extends Object implements AccountCache
Caches important (but small) account state to avoid database hits.

This class should be bounded as a Singleton. However, due to internal limitations in Google, it cannot be marked as a singleton. The common installation pattern should therefore be:


 install(AccountCacheImpl.module());
 install(AccountCacheImpl.bindingModule());
 
  • Method Details

    • module

      public static com.google.inject.Module module()
    • bindingModule

      public static com.google.inject.Module bindingModule()
    • getEvenIfMissing

      public AccountState getEvenIfMissing(Account.Id accountId)
      Description copied from interface: AccountCache
      Returns an AccountState instance for the given account ID. If not cached yet the account is loaded. Returns an empty AccountState instance to represent a missing account.

      This method should only be used in exceptional cases where it is required to get an account state even if the account is missing. Callers should leave a comment with the method invocation explaining why this method is used. Most callers of AccountCache should use AccountCache.get(Account.Id) instead and handle the missing account case explicitly.

      Specified by:
      getEvenIfMissing in interface AccountCache
      Parameters:
      accountId - ID of the account that should be retrieved
      Returns:
      AccountState instance for the given account ID, if no account with this ID exists an empty AccountState instance is returned to represent the missing account
    • get

      public Optional<AccountState> get(Account.Id accountId)
      Description copied from interface: AccountCache
      Returns an AccountState instance for the given account ID. If not cached yet the account is loaded. Returns Optional.empty() if the account is missing.
      Specified by:
      get in interface AccountCache
      Parameters:
      accountId - ID of the account that should be retrieved
      Returns:
      AccountState instance for the given account ID, if no account with this ID exists Optional.empty() is returned
    • getFromMetaId

      public AccountState getFromMetaId(Account.Id id, org.eclipse.jgit.lib.ObjectId metaId)
      Description copied from interface: AccountCache
      Returns an AccountState instance for the given account ID at the given metaId of RefNames.refsUsers(com.google.gerrit.entities.Account.Id) ref.

      The caller is responsible to ensure the presence of metaId and the corresponding meta ref. The method does not populate AccountState.defaultPreferences().

      Specified by:
      getFromMetaId in interface AccountCache
      Parameters:
      id - ID of the account that should be retrieved.
      metaId - the sha1 of commit in RefNames.refsUsers(com.google.gerrit.entities.Account.Id) ref.
      Returns:
      AccountState instance for the given account ID at specific sha1 metaId.
    • get

      public Map<Account.Id,AccountState> get(Set<Account.Id> accountIds)
      Description copied from interface: AccountCache
      Returns a Map of Account.Id to AccountState for the given account IDs. If not cached yet the accounts are loaded. If an account can't be loaded (e.g. because it is missing), the entry will be missing from the result.

      Loads accounts in parallel if applicable.

      Specified by:
      get in interface AccountCache
      Parameters:
      accountIds - IDs of the account that should be retrieved
      Returns:
      Map of Account.Id to AccountState instances for the given account IDs, if an account can't be loaded (e.g. because it is missing), the entry will be missing from the result
    • getByUsername

      public Optional<AccountState> getByUsername(String username)
      Description copied from interface: AccountCache
      Returns an AccountState instance for the given username.

      This method first loads the external ID for the username and then uses the account ID of the external ID to lookup the account from the cache.

      Specified by:
      getByUsername in interface AccountCache
      Parameters:
      username - username of the account that should be retrieved
      Returns:
      AccountState instance for the given username, if no account with this username exists or if loading the external ID fails Optional.empty() is returned