scistag.common.disk_cache.DiskCache

class DiskCache(version=1, cache_dir=None)[source]

Bases: object

Helper class to persist data such as computation results on disk.

This class is usually not used directly, see Cache which 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
  • version (int) –

    The cache version. 1 by default.

    When ever you change this version all old cache values will be removed and/or ignored from the cache.

  • cache_dir (str | None) – The directory in which the data shall be cached

Methods

delete

Deletes a single cache entry

encode_name

Encodes the name of the object to be cached to a unique hash

get

Tries to read an element from the disk cache.

get_cache_name

Encodes the name of the object to be cached to a unique hash

set

Persists a single value in the cache

Attributes

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

version

Returns the cache version

_version

The cache version.

valid

Defines if the cache is valid

_ensure_cache_dir()[source]

Verifies the caching directory is present

delete(key)[source]

Deletes a single cache entry

Parameters

key – The cache’s key

Return type

bool

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

  • version (int | str) – The cache version for this entry.

  • 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

property version: int

Returns the cache version

Return type

int