scistag.common.data_cache.DataCache

class DataCache(thread_safe=False)[source]

Bases: object

A 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

cache

Tries to retrieve given element from cache and generates it's data otherwise.

clear

Clears the whole cache

get

Returns an item if it exists

remove

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)

global_cache

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

clear()[source]

Clears the whole cache

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

remove(names)[source]

Removes one or multiple elements from the cache :type names: str | list[str] :param names: The list of names

Return type

None

global_cache: DataCache | None = <scistag.common.data_cache.DataCache object>

The global cache dictionary to temporary store computations.

Use with care and always prefer session or slide specific caches for automatic unloading cached data when it’s not needed anymore.