duck.utils.caching

Caching module which leverages the use of diskcache python library. Essential methods mandatory to any Cache class: [set, get, delete, clear]

Package Contents

Classes

CacheBase

Base class for caching

CacheSpeedTest

This class performs speed test of Cache classes.

DynamicFileCache

Manages a cache of files, dynamically creating new files when existing ones reach a size limit.

InMemoryCache

InMemoryCache with LRU eviction.

KeyAsFolderCache

Caching which stores cache data in folders with the name of cache keys.

PersistentFileCache

Implementation of caching using the diskcache library

API

class duck.utils.caching.CacheBase[source]

Base class for caching

abstractmethod clear()[source]

Clear all values from the cache.

abstractmethod delete(key: str)[source]

Delete a value from the cache.

abstractmethod get(key: str) Any[source]

Get a value from the cache. Returns None if the key is not found.

save()[source]
abstractmethod set(key: str, value: Any, expiry: int | float = None)[source]

Set a value in the cache.

class duck.utils.caching.CacheSpeedTest(repeat: int = 1)[source]

This class performs speed test of Cache classes.

Initialization

compare_performance()[source]
execute_all()[source]
static generate_random_string(length)[source]
instances

None

print_summary()[source]
run_test(instance)[source]
test_clear(instance)[source]
test_create(instance)[source]
test_del(instance)[source]
test_get(instance)[source]
test_set(instance)[source]
class duck.utils.caching.DynamicFileCache(cache_dir: str, cache_limit=1000000000.0, cached_objs_limit: int = 128)[source]

Bases: duck.utils.caching.CacheBase

Manages a cache of files, dynamically creating new files when existing ones reach a size limit.

Initialization

_create_new_cache_path()[source]

This retrieves new cache path with a unique name using uuid module.

_get_cache_path() str[source]

Returns the path to a cache dir that is not at the size limit.

_reload_cache_files()[source]

This reloads existing cache files in a directory.

property cache_obj

Returns the Cache object for the current cache file that is not at its limit.

clear()[source]

Clears all data from the cache.

close()[source]

Close the cache.

delete(key: str)[source]

Delete a key pair from the cache.

get(key: str) Any[source]

Retrieve cache data.

static get_cache_obj(path: str) duck.utils.caching.PersistentFileCache[source]
set(key: str, data: Any, expiry: float | int = None)[source]

Set cache data with persistence.

class duck.utils.caching.InMemoryCache(maxkeys=None, *_)[source]

Bases: duck.utils.caching.CacheBase

InMemoryCache with LRU eviction.

Initialization

clear()[source]
close()[source]
delete(key: str)[source]
get(key: str, default: Any = None, pop: bool = False) Any[source]

Get a value from the cache.

has(key: str)[source]
set(key: str, value: Any, expiry=None)[source]

Set a value in the cache.

class duck.utils.caching.KeyAsFolderCache(cache_dir: str)[source]

Bases: duck.utils.caching.CacheBase

Caching which stores cache data in folders with the name of cache keys.

Initialization

clear()[source]

Clear all data from the cache.

close()[source]

Closes the cache.

delete(key: str)[source]

Delete a key pair from the cache.

get(key: str) Any[source]

This lookup for a folder in cache_dir with the name of the parsed key and returns the cache data.

static get_cache_files(d: str)[source]

This gets the directories in cache_dir.

static get_cache_obj(path: str) duck.utils.caching.PersistentFileCache[source]
set(key: str, data: Any, expiry: int | float = None)[source]

Set some cache data.

class duck.utils.caching.PersistentFileCache(path: str, cache_size: int = None)[source]

Bases: duck.utils.caching.CacheBase

Implementation of caching using the diskcache library

Initialization

clear()[source]

Clears all data from the cache.

close()[source]

This closes the cache.

property closed

Checks whether the cache is closed.

delete(key: str)[source]

Delete a key from the cache.

get(key: str)[source]

Retrieves the key value from cache.

set(key: str, _obj: Any, expiry: int | float = None)[source]

Sets the cache with an optional expiry in seconds.