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 notNone
. Ifget
returnsNone
, the caller should take the lock, callget
again, and then allocate andstore
a token ifget
still returnsNone
.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
- get(name)¶
Retrieve the UID or GID for a name, if available.
- 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:
- store(name, id)¶
Store the UID or GID for a user or group in the cache.
- Parameters:
- Return type:
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)