Caching utilities#
- class commons.caching.TimedCache(*, global_ttl: timedelta | None = None, lazy_eviction: bool = True, ttl_from_last_access: bool = False)#
- __init__(*, global_ttl: timedelta | None = None, lazy_eviction: bool = True, ttl_from_last_access: bool = False)#
- Parameters:
global_ttl (Optional[timedelta]) – A default TTL for any added entries.
lazy_eviction (bool) –
Whether this cache should perform lazy eviction or not.
Defaults to True
ttl_from_last_access (bool) –
Whether the TTL of an object is dictated by the last access or time of insertion.
This requires a global TTL to be set.
Defaults to True, time of insertion.
- add_entry(key: KT, value: VT, *, ttl: timedelta | None = None, override: bool = False) None#
Add an entry to the cache.
- Parameters:
key – The key to store this under.
value – The item you want to store in the cache
ttl (Optional[timedelta]) – An optional period of time to expire this entry after.
override (bool) – Whether or not to override an existing value
- Raises:
ExistingEntry – You are trying to insert a duplicate key
Notes
ttl passed to this method will take precendence over the global ttl.
- get_entry(key: KT) VT#
Fetch a value from the cache
- Parameters:
key – The key you wish to retrieve a value for
- Returns:
The provided value
- Return type:
VT
- Raises:
NonExistentEntry – No value exists in the cache for the provided key.
- class commons.caching.Entry(value: Any, expiry_time: datetime.datetime | None = None)#
- class commons.caching.NonExistentEntry#
No entry found in the cache with this key.
- class commons.caching.ExistingEntry#
An entry was already found in the cache with this key.