scistag.common.cache.Cache¶
- class Cache(version=1, cache_dir=None)[source]¶
Bases:
objectThe Cache class shall help caching computation results, downloaded data but also objects with large wind-up time (such as neural networks) between execution sessions.
If you pass a version of 0 the
get_app_session_id()will be used instead which will usually change on every restart except you set it yourself via set_app_session_id or are using a smart autoreloader such as VisualLog’s auto-reload capabilities which ensure this for you.- Parameters
Methods
Adds a member to the volatile cache entry variable list so it can automatically be cleared upon unloading of this component.
Tries to find the element key in the cache and return's it's content.
Returns a value from the cache.
Returns the app session id which is generated upon each application restart.
Returns if the component is currently being loaded :rtype:
bool:return: True ifloadis currently being executed for this component.Returns the effective key, version combination to search for a key in combination with the cache's and the element's version.
Event handling function for dynamically loading data on demand.
Event handler for unloading elements previously loaded in your handle_load function.
Call this before you start using a component for the first time.
Manually overrides the application's session ID.
Adds an item to the cache or updates it.
Call this to unload all data from your component which was created during the handle_load execution and not flagged via a slash ("/") in its name as element to cache on disk.
Attributes
__annotations____dict____doc____module____weakref__list of weak references to the object (if defined)
The current run's session ID.
Returns the cache version
Multithreading access lock
A cache for temporary data storage and objects with a life time bound to this component's usage state.
Version numbers for the elements stored in the memory cache
Parameters for the elements stored in the memory cache
Cache for persisting data between execution sessions
The cache version.
Defines if the component was correctly loaded
Flag which tells if the component is currently being loaded and if new values added to the cache via
self["objectName"]shall be flagged as volatile.Stores which cache entries shall be deleted upon the execution of
unload().- __contains__(key)[source]¶
Returns if an element exists in the cache.
- Parameters
key – The item’s name
- Return type
- Returns
True if the item exists.
- __delitem__(key)[source]¶
Deletes an element from the cache.
If the element does not exist a ValueError exception will be raised.
- Parameters
key – The element’s name
- add_volatile_member(name)[source]¶
Adds a member to the volatile cache entry variable list so it can automatically be cleared upon unloading of this component.
When the component is unloaded (e.g. because a Widget or a Slide disappears) all members are automatically set to None to prevent that these objects are kept alive.
- Parameters
name (
str) – The name of the member variable to be added to the volatile
list.
- Return type
- cache(key, generator, *args, **kwargs)[source]¶
Tries to find the element key in the cache and return’s it’s content. If no element with such a name and/or version number can be found generator be called to generate the data, store it in the cache for the next execution.
- Parameters
key (
str) – The key of the cache elementgenerator (
Callable) – The function to call if the element is not stored in the cache yet.version –
The version to assign to the cache entry. If either this or the cache’s main version get modified all prior cache entries will be ignored until they were update to this new version.
Default version for memory cache entries is 0, for disk cache entries 1.
hash_val – A single value, a list of values or a dict of values of which a hash value is computed and added to the version number to automatically invalidate it if any of the values changed.
args – Argument parameters to be passed into the generator
kwargs – Keyword parameter to be passed into the generator
- Returns
The cached or newly created content
- get(key, default=None, version=None, params=None, hash_params=False)[source]¶
Returns a value from the cache.
If the element does not exist a ValueError exception will be raised.
- Parameters
key (str) – The item’s name.
version –
The cache element’s version.
By default 0 for memory cache elements and 1 for disk cache elements.
params (dict | None) – The parameters which were passed into the function and still need t match.
hash_params – Defines the parameters shall be hashed to detect modifications.
default – The value to return by default if no cache element could be found.
- Returns
The item’s value.
Returns default if no value can be found.
- classmethod get_app_session_id()[source]¶
Returns the app session id which is generated upon each application restart.
- Return type
- get_is_loading()[source]¶
Returns if the component is currently being loaded :rtype:
bool:return: True ifloadis currently being executed for this component.
- classmethod get_key_and_version(key, major, minor=None)[source]¶
Returns the effective key, version combination to search for a key in combination with the cache’s and the element’s version.
- Parameters
key – The key (potentially still containing @version at it’s end), either “key” or “key@version”, e.g. “myDb@2”.
major – The major version (provided by the cache object)
minor –
The minor version, per element.
Default 0 for memory cache elements and 1 for disk cache entries.
- Return type
- Returns
The effective key and version with which the element shall be persisted and which we assume upon restore.
Following rules apply: - minor >= 1: The cache’s version and the minor version are
combined. So if you change either the cache’s or the element version the element gets invalidated.
- minor <= -1: The data should be handled as “constant” which
basically never changes. Changing the cache’s version has thus no effect, only if you directly change the element’s version.
minor equals 0: The session’s ID will be used as minor version.
This invalidates all cache entries when you completely close the app or service (including auto-restart mechanisms, e.g. for live-editing).
- handle_load()[source]¶
Event handling function for dynamically loading data on demand.
SciStag’s
loadandunloadmechanism shall help minimizing the memory footprint of the application using it. If you have temporary data, for example a database which is just used while a component is used, a Slide or an ImageView while they are visible please overwrite this function, call it’s ancestor and then store your data in the “cache”.You can do so by using the bracket operator like
self['db'] = pd.read_csv(...), check if data is available viaif 'db' in self: ...and access it viamy_db = self['db']accordingly.All data stored this way will automatically get cleared and removed from the cache when the unload function is called, e.g. when the Slide or Widget disappears or when you call it for your custom component.
If you want to use member variables for storing your temporary data you can do so by calling
add_volatile_member()and passing their name. Upon unloadingNonewill be assigned to all registered variables.Note: When overwriting this method call
super().handle_load()at the beginning of yours.
- handle_unload()[source]¶
Event handler for unloading elements previously loaded in your handle_load function.
Note: When overwriting this method call
super().handle_unload()at end beginning of yours.
- load()[source]¶
Call this before you start using a component for the first time. The
scistag.slidestag.widget.Widgetclass does this automatically for all of its children when a Widget becomes visible.
- classmethod override_app_session_id(session_id)[source]¶
Manually overrides the application’s session ID.
Should only be used by application session managers.
- Parameters
session_id (
int) – The new session id
- set(key, value, params=None, version=None)[source]¶
Adds an item to the cache or updates it.
If a value is added (for the first time) during the execution of the component’s
load()orhandle_loadmethod it is flagged as volatile and will be automatically removed again upon the execution ofunload()/handle_unload().- Parameters
key (str) – The item’s name
value – The value to assign
params (dict | None) – The parameters associated with the creation of value and to be stored (and verified) in combination with the version.
version – The version of the cache element. By default 0 for memory cache elements and 1 for disk cache elements.
- unload()[source]¶
Call this to unload all data from your component which was created during the handle_load execution and not flagged via a slash (“/”) in its name as element to cache on disk.
- _access_lock¶
Multithreading access lock
- _app_session_id: int = 1668323886284¶
The current run’s session ID. is updated every re-start usually, may be stored and restored by helper classes such as the VisualLogAutoReloader between application restarts.
- _disk_cache¶
Cache for persisting data between execution sessions
- _is_loading¶
Flag which tells if the component is currently being loaded and if new values added to the cache via
self["objectName"]shall be flagged as volatile.During the execution of the
load()function and their event handlers such asWidget.handle_load()this value is set to true. When new Widgets are added during this time or new values are added to the cache, e.g. via my_component[“data”] = load_data() they are flagged as volatile.After the execution of unload() all volatile entries will be deleted from the cache and all widgets added as child will be automatically removed again.
- _mem_cache¶
A cache for temporary data storage and objects with a life time bound to this component’s usage state.
A cache which whose content shall only live between a handle_load() and handle_unload(). This can be used to store data only while a component is being used, a Widget is visible etc. See
handle_load().
- _mem_cache_params¶
Parameters for the elements stored in the memory cache
- _mem_cache_versions¶
Version numbers for the elements stored in the memory cache
- _version¶
The cache version.
It is stored along all cache values stored on disk and only elements sharing the same version will be accepted.
- _volatile_cache_entries¶
Stores which cache entries shall be deleted upon the execution of
unload().If a cache entry is added while
_is_loadingis set to True it will be added to this set. Upon the execution ofunload()all elements named in this list will be removed from the_cache.In addition you may add object member variables via
add_volatile_member()which do not get removed but automatically cleared upon execution of unload.
- loaded¶
Defines if the component was correctly loaded