scistag.common.data_cache.DataCache¶
- class DataCache(thread_safe=False)[source]¶
Bases:
objectA class which temporarily caches values, e.g. a loaded DataFrame or a rendered text using a simple “execute function if the value does not exist yet” to easily process data on demand only.
Initializer
- Parameters
thread_safe (
bool) – Defines if the cache has to be thread safe. False by default for UI / linear execution caches such as in SlideStag.
Methods
Tries to retrieve given element from cache and generates it's data otherwise.
Clears the whole cache
Returns an item if it exists
Removes one or multiple elements from the cache :type names: str | list[str] :param names: The list of names
Attributes
__annotations____dict____doc____module____weakref__list of weak references to the object (if defined)
The global cache dictionary to temporary store computations.
- __contains__(item)[source]¶
Returns if a specific item exists
- Parameters
item – The item’s name
- Returns
True if it does
- __getitem__(item)[source]¶
Returns an item if it exists
- Parameters
item – The item’s name
- Return type
Any | None
- Returns
The item’s data
- cache(identifier, builder, parameters=None, overwrite=False)[source]¶
Tries to retrieve given element from cache and generates it’s data otherwise.
If the element is not stored
in the cache it will be created using the builder callback which should await a single parameter and return a single value. If parameters (optional) is passed it will be verified if the parameters were modified.
- Parameters
identifier (str) – The identifier. Either a string or a dictionary with a configuration.
builder (Callable[[Any], Any] | None) – The function to call if the value does not exist
parameters (Any | None) – If the data may dynamically change using the same identifier, pass it too
overwrite (bool) – If set to true the previous value will be replaced
- Return type
Any
- Returns
The data
- get(item, default=None)[source]¶
Returns an item if it exists
- Parameters
item (str) – The item’s name
:param default; The default value :rtype: Any | None :return: The item’s data if it exists, returns default otherwise