Caching

Cached lookups of accounts, users and user profiles, and the commonly used objects of the Smarter account.

Like each app’s caching module (e.g. smarter.apps.llmclient.caching), this holds the account app’s cached reads, through @cache_results:

  • smarter_cached_objects, the Smarter account, its admin and the platform admin, lazily loaded.

  • get_cached_* functions: the default account, a user’s account, a user by id or by username, a user’s avatar url, and an account’s admin user, which get_cached_admin_user_for_account() creates if the account has none.

smarter.apps.account.caching.ACCOUNT_ADMIN_USERNAME_SUFFIX = '-admin'

The username of the admin user that get_cached_admin_user_for_account() creates is its account number plus this.

class smarter.apps.account.caching.SmarterCachedObjects[source]

Bases: object

Lazy instantiations of cached objects for the smarter account.

This is a much-simplified means of caching commonly used objects without having to actually decorate every function that fetches them.

Raises:

SmarterConfigurationError – If the smarter account or admin user cannot be found.

Note

This class uses lazy loading to fetch and cache the smarter account and admin user only when accessed.

__init__()[source]
property admin_user: User

Retrieve the admin user instance for the smarter account.

Lazy loads and caches the user on first access. Subsequent accesses will return the cached user, with a periodic refresh from the database to ensure data consistency.

Returns:

User instance representing the admin user.

Raises:

SmarterConfigurationError – If the admin user cannot be found.

property smarter_account: Account

Retrieve the smarter account instance.

Returns:

Account instance representing the smarter account.

Raises:

SmarterConfigurationError – If the smarter account cannot be found.

property smarter_admin: User

Retrieve the smarter admin user instance.

Returns:

User instance representing the smarter admin.

Raises:

SmarterConfigurationError – If the smarter admin user cannot be found.

property smarter_admin_user_profile: UserProfile

Retrieve the UserProfile instance for the smarter admin user.

Lazy loads and caches the UserProfile on first access. Subsequent accesses will return the cached UserProfile, with a periodic refresh from the database to ensure data consistency.

Returns:

UserProfile instance for the smarter admin user.

Raises:

SmarterConfigurationError – If the UserProfile cannot be found.

smarter.apps.account.caching.account_admin_username(account_number)[source]

The username of the admin user that get_cached_admin_user_for_account() creates for an account.

Parameters:

account_number (str) – The account’s number, e.g. 1234-5678-9012.

Return type:

str

Returns:

e.g. 1234-5678-9012-admin.

smarter.apps.account.caching.get_cached_account_for_user(invalidate=False, user=None)[source]

Locate the Account associated with a given user, using caching for performance.

The account is that of the UserProfile that the user chose in the web console’s user menu, for this request (see UserProfile.get_selected_object()), else the default account, if the user belongs to it, else the account of the user’s first profile.

Parameters:
  • user (User | AnonymousUser | None) – User instance. The user whose account should be located. Any other user, such as an AnonymousUser, has no account.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

Account

Returns:

Account instance if found, otherwise None.

Warning

If no account is found for the user, None is returned and a warning is logged.

Tip

Use invalidate=True after updating user or account data to ensure cache consistency.

Example usage:

# Locate account for a user
account = get_cached_account_for_user(user)
# Invalidate cache before fetching
account = get_cached_account_for_user(user, invalidate=True)
smarter.apps.account.caching.get_cached_admin_user_for_account(account, invalidate=False)[source]

Retrieve the admin user for a given account, creating one if necessary.

Parameters:
  • account (Account) – Account instance. The account for which to retrieve the admin user.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

User

Returns:

User instance representing the account admin.

Important

If no admin user exists for the account, a new staff user and UserProfile will be created automatically.

Warning

If the account is missing or misconfigured, an exception is raised.

Tip

Use invalidate=True after updating admin user data to ensure cache consistency.

Example usage:

# Retrieve the admin user for an account
admin_user = get_cached_admin_user_for_account(account=account)

# Invalidate cache before fetching
admin_user = get_cached_admin_user_for_account(invalidate=True, account=account)
smarter.apps.account.caching.get_cached_avatar_url(user_id, invalidate=False)[source]

Return the url of the image that represents a user in the web console, using caching for performance.

The url is UserProfile.avatar_url: the image the user uploaded, else the one the oauth provider supplied, else the default profile image. A user with no UserProfile gets the default profile image. The UserProfile is the one that the user chose in the web console’s user menu, for this request, else their newest.

Parameters:
  • user_id (int) – Integer. The primary key of the user.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

str

Returns:

The image’s url.

Tip

Use invalidate=True after changing a user’s profile image, so that the console shows it at once.

Example usage:

url = get_cached_avatar_url(user_id=request.user.id)
smarter.apps.account.caching.get_cached_default_account(invalidate=False)[source]

Retrieve the default account instance, using caching for performance.

Parameters:

invalidate (bool) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

Account

Returns:

The default Account instance.

Important

The default account is determined by the is_default_account=True flag in the database.

Warning

If no default account exists, an exception may be raised.

Example usage:

# Get the default account
default_account = get_cached_default_account()

# Invalidate cache before fetching
default_account = get_cached_default_account(invalidate=True)
smarter.apps.account.caching.get_cached_smarter_admin_user_profile()[source]

Retrieve the admin UserProfile for the smarter account, using caching for performance.

Return type:

UserProfile

Returns:

UserProfile instance for the smarter admin user.

Note

The smarter admin user is typically a superuser or staff user associated with the platform’s main account.

Example usage:

# Retrieve the smarter admin user profile
admin_profile = get_cached_smarter_admin_user_profile()

# Invalidate cache before fetching
admin_profile = get_cached_smarter_admin_user_profile(invalidate=True)
smarter.apps.account.caching.get_cached_user_for_user_id(invalidate=False, user_id=None)[source]

Retrieve a User instance by its primary key, using caching for performance.

Parameters:
  • user_id (int | None) – Integer. The primary key of the user to retrieve.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

User

Returns:

User instance if found, otherwise None.

Warning

If no user exists for the given ID, None is returned and an error is logged.

Tip

Use invalidate=True after updating user data to ensure cache consistency.

Example usage:

# Retrieve user by ID
user = get_cached_user_for_user_id(user_id=123)

# Invalidate cache before fetching
user = get_cached_user_for_user_id(user_id=123, invalidate=True)
smarter.apps.account.caching.get_cached_user_for_username(invalidate=False, username=None)[source]

Retrieve a User instance by its username, using caching for performance.

Parameters:
  • username (str | None) – String. The username of the user to retrieve.

  • invalidate (bool | None) – Boolean, optional. If True, invalidates the cache before fetching.

Return type:

User

Returns:

User instance if found, otherwise None.

Warning

If no user exists for the given username, a User.DoesNotExist exception is raised and an error is logged.

Tip

Use invalidate=True after updating user data to ensure cache consistency.

Example usage:

# Retrieve user by username
user = get_cached_user_for_username("johndoe")

# Invalidate cache before fetching
user = get_cached_user_for_username("johndoe", invalidate=True)
smarter.apps.account.caching.smarter_cached_objects = <smarter.apps.account.caching.SmarterCachedObjects object>

Smarter_cached_objects = SmarterCachedObjects().

An instance of SmarterCachedObjects for accessing commonly used cached objects. Functions as a singleton for the project.