djblets.cache.synchronizer¶
State synchronization via caches.
- class GenerationSynchronizer(cache_key: str, *, normalize_cache_key: bool = True)¶
Bases:
object
Manages the synchronization of generation state across processes.
This is a utility class that makes it easy for consumers to synchronize a generation ID between processes and servers. This ID can be used to check whether a process has the latest version of some data, updating if the version it has is expired.
Callers should create a
GenerationSynchronizer
with a stable cache key, which will be used for communication between processes/servers. The initial generation number will be fetched, or created if one does not already exist.When the caller has updated something in the state, it must call
mark_updated()
. This will bump the synchronization generation number, which will invalidate other processes.Other callers, upon noticing that their state is expired (through
is_expired()
) can re-fetch or re-compute the data needed and then callrefresh()
to refresh the instance’s counter from the cache.- __init__(cache_key: str, *, normalize_cache_key: bool = True) None ¶
Initialize the synchronizer.
Changed in version 5.1:
normalize_cache_key
is now a keyword-only argument. This will be enforced in Djblets 7.- Parameters:
cache_key (
str
) – The base cache key used for all synchronization. This will be normalized bymake_cache_key()
.normalize_cache_key (
bool
, optional) – Whether to normalize the cache key. Normalizing it will ensure it can fit within the key length constraints, and reduces changes of colliding with keys from other services. This is enabled by default.
- is_expired() bool ¶
Return whether the current state has expired.
- Returns:
True
if the state has expired.False
if this has the latest cached generation.- Return type:
- refresh() None ¶
Refresh the generation ID from cache.
This should be called after having updated to the latest state.
- clear() None ¶
Clear the cached generation ID.
This will expire all existing caches and force all processes to re-fetch and store the cache state.
- mark_updated() None ¶
Mark the synchronized state as having been updated.
All other processes will find their state expired, and will need to re-update.
- __annotations__ = {'cache_key': 'str', 'sync_gen': 'Optional[int]'}¶