caching
Contains classes designed for easy caching/persistence.
These classes are designed to be used in cases where you would normally use a dict
but want to have the ability to persist between restarts/runs or do not/cannot store
the entire dict in memory.
Module
Functions
get_cache
def get_cache( *, cache_name: Optional[str] = None, datasource_name: Optional[str] = None, project_id: Optional[str] = None, task_id: Optional[str] = None, use_tmp_dir: bool = False,) ‑> Cache:
Create/retrieve cache given various name/specifier options.
At least one specifier option must be provided.
If a cache with the same specifiers exists, it will be opened and returned. If one does not exist it will be created.
Specifiers should be provided when a cache that is associated with a given datasource/task/project is wanted. For instance, if a cache is wanted for a given datasource for a given project, both datasource_name and project_id should be provided. Such a cache would be shared across all task runs within that project when using that datasource.
Arguments
cache_name
: Human-provided name/additional specifier for cache.datasource_name
: Name of datasource this cache is related to.project_id
: UUID of the project this cache is related to.task_id
: UUID of the task run this cache is related to.use_tmp_dir
: Whether to use a temporary directory for the cache. If this is true then the cache is unlikely to be reusable across runs.
Returns Cache instance named for this combination of specifiers.
Raises
ValueError
: if no specifiers are provided.
Classes
Cache
class Cache(cache_name: str):
An on-disk cache/persistence class.
This can be used as though a standard mutable mapping.
Ancestors
Subclasses
- bitfount.persistence.caching._DiskcacheCache
Methods
add
def add(self, k: str) ‑> None:
Add key to cache.
Stores key with a sentinel value so that key can be checked for via key in cache
but where the value does not matter.
close
def close(self) ‑> None:
Close the cache.
delete
def delete(self, k: str, error: bool = False) ‑> None:
Delete key from cache.
Raises
KeyError
: if key is not present anderror
is True
delete_cache
def delete_cache(self) ‑> None:
Delete the cache.
set
def set(self, k: str, v: _JSON) ‑> None:
Set a value against key.
EncryptedDiskcacheFunctionCache
class EncryptedDiskcacheFunctionCache(json_compression_level: int = 6):
An encrypted function cache implementation using diskcache.
This class provides a secure way to cache function results, with both keys and values encrypted on disk.
Initialize the EncryptedDiskcacheFunctionCache.
Arguments
json_compression_level
: The compression level for JSON data (default: 6).
Ancestors
Methods
clear
def clear(self) ‑> None:
Inherited from:
Clear all entries from the cache.
This method removes all memoized results from the cache, effectively resetting it.
memoize
def memoize( self, expire: Optional[float] = 21600, ignore: Container[int | str] = (),) ‑> Callable[[Callable[~_P, ~_T]], Memoized[~_P, ~_T]]:
Inherited from:
Memoize a function, caching its results.
This method returns a decorator that can be applied to functions to cache their results.
Arguments
expire
: The time-to-live for cached entries in seconds. If None, entries do not expire.ignore
: A container of argument indices or names to ignore when creating the cache key. Note that you may need to pass both the index and the keyword name if the argument can be passed by both.
Returns A decorator function that, when applied to a function, returns a memoized version of that function.
FunctionCache
class FunctionCache():
A cache for applying to function calls à la functools.cache().
Subclasses
Methods
clear
def clear(self) ‑> None:
Clear all entries from the cache.
This method removes all memoized results from the cache, effectively resetting it.
memoize
def memoize( self, expire: Optional[float] = 21600, ignore: Container[int | str] = (),) ‑> Callable[[Callable[~_P, ~_T]], Memoized[~_P, ~_T]]:
Memoize a function, caching its results.
This method returns a decorator that can be applied to functions to cache their results.
Arguments
expire
: The time-to-live for cached entries in seconds. If None, entries do not expire.ignore
: A container of argument indices or names to ignore when creating the cache key. Note that you may need to pass both the index and the keyword name if the argument can be passed by both.
Returns A decorator function that, when applied to a function, returns a memoized version of that function.
Memoized
class Memoized(*args, **kwargs):
A protocol for memoized functions.
This protocol defines the interface for memoized functions, including methods for generating cache keys and calling the memoized function.