IdCache

class gafaelfawr.cache.IdCache

Bases: BaseCache

A cache of UIDs or GIDs.

This contains only the data structure for the ID cache and some simple accessor functions. All of the logic is handled by FirestoreService. Two instances of this class will be created, one for UIDs and one for GIDs.

UIDs and GIDs, once cached, are immutable, so the caller can first call get without a lock and safely use the result if it is not None. If get returns None, the caller should take the lock, call get again, and then allocate and store a token if get still returns None.

Notes

When there’s a cache miss for a UID or GID, the goal is to block the expensive Firestore API call until the first requester either finds a token in the database or creates a new one, either way adding it to the cache. Hopefully then subsequent requests that were blocked on the lock can be answered from the cache.

Methods Summary

clear()

Invalidate the cache.

get(name)

Retrieve the UID or GID for a name, if available.

lock()

Return the cache lock for use in a context manager.

store(name, id)

Store the UID or GID for a user or group in the cache.

Methods Documentation

async clear()

Invalidate the cache.

Used primarily for testing.

Return type:

None

get(name)

Retrieve the UID or GID for a name, if available.

Parameters:

name (str) – Username or group name.

Returns:

UID or GID if the name is in the cache, else None.

Return type:

int or None

lock()

Return the cache lock for use in a context manager.

See store for how to use this method.

Returns:

The lock for the cache.

Return type:

asyncio.Lock

store(name, id)

Store the UID or GID for a user or group in the cache.

Parameters:
  • name (str) – Name of the user or group.

  • id (int) – UID or GID to store.

Return type:

None

Examples

async with id_cache.lock():
    uid = id_cache.get(username)
    if not uid:
        # do something to allocate a UID
        id_cache.store(username, uid)