scistag.common.disk_cache.DiskCache¶
- class DiskCache(version=1, cache_dir=None)[source]¶
Bases:
objectHelper class to persist data such as computation results on disk.
This class is usually not used directly, see
Cachewhich makes use of the DiskCache. All elements you store with a beginning “./” using the Cache class will automatically be stored on disk, all entries without in memory.- Parameters
Methods
Deletes a single cache entry
Encodes the name of the object to be cached to a unique hash
Tries to read an element from the disk cache.
Encodes the name of the object to be cached to a unique hash
Persists a single value in the cache
Attributes
__dict____doc____module____weakref__list of weak references to the object (if defined)
Returns the cache version
The cache version.
Defines if the cache is valid
- delete(key)[source]¶
Deletes a single cache entry
- Parameters
key – The cache’s key
- Return type
- Returns
True if the element was found and deleted
- static encode_name(name)[source]¶
Encodes the name of the object to be cached to a unique hash
- Parameters
name – The name of the data
- Returns
The encoded name
- get(key, params=None, version=1, hash_params=False, default=None)[source]¶
Tries to read an element from the disk cache.
- Parameters
key – The name of the object to load from cache or a combination of key and version separated by an @ sign, e.g. “database@1”
params (dict) – The creation parameters which were passed into the loading function and still should match.
version (int | str) – The assumed version of this element we are searching for. If the version does not match the old entry is ignored.
hash_params (bool) – Defines the parameters shall be hashed to detect modifications.
default – The default value to return if no cache entry could be found
- Return type
Any | None
- Returns
Either the cache data or the default value as fallback
- get_cache_name(name)[source]¶
Encodes the name of the object to be cached to a unique hash
- Parameters
name – The name of the data
- Returns
The encoded name
- set(key, value, params, version=1, hash_params=False)[source]¶
Persists a single value in the cache
- Parameters
key (str) – The name of the object to cache or a combination of key and version separated by an @ sign, e.g. “database@1”
value (Any) – The element’s value
params (dict) – The creation parameters which were passed into the loading function and should match upon a cache fetch try.
hash_params (bool) – Defines the parameters shall be hashed to detect modifications.
- _version¶
The cache version.
It is stored along all cache values stored on disk and only elements sharing the same version will be accepted.
- valid¶
Defines if the cache is valid