cache#
Source code: sensai/util/cache.py
- class BoxedValue(value: TValue)[source]#
Bases:
Generic
[TValue
]Container for a value, which can be used in caches where values may be None (to differentiate the value not being present in the cache from the cached value being None)
- class KeyValueCache(*args, **kwds)[source]#
Bases:
Generic
[TKey
,TValue
],ABC
- class InMemoryKeyValueCache[source]#
Bases:
KeyValueCache
[TKey
,TValue
],Generic
[TKey
,TValue
]A simple in-memory cache (which uses a dictionary internally).
This class can be instantiated directly, but for better typing support, one can instead inherit from it and provide the types of the key and value as type arguments. For example for a cache with string keys and integer values:
class MyCache(InMemoryKeyValueCache[str, int]): pass
- set(key: TKey, value: TValue)[source]#
Sets a cached value
- Parameters:
key – the key under which to store the value
value – the value to store; since None is used indicate the absence of a value, None should not be used a value
- class PersistentKeyValueCache(*args, **kwds)[source]#
Bases:
KeyValueCache
[TKey
,TValue
],Generic
[TKey
,TValue
],ABC
- class PersistentList(*args, **kwds)[source]#
Bases:
Generic
[TValue
],ABC
- class DelayedUpdateHook(fn: Callable[[], Any], time_period_secs, periodically_executed_fn: Optional[Callable[[], Any]] = None)[source]#
Bases:
object
Ensures that a given function is executed after an update happens, but delay the execution until there are no further updates for a certain time period
- Parameters:
fn – the function to eventually call after an update
time_period_secs – the time that must pass while not receiving further updates for fn to be called
periodically_executed_fn – a function to execute periodically (every timePeriodSecs seconds) in the busy waiting loop, which may, for example, log information or apply additional executions, which must not interfere with the correctness of the execution of fn
- class PeriodicUpdateHook(check_interval_secs: float, no_update_time_period_secs: Optional[float] = None, no_update_fn: Optional[Callable[[], Any]] = None, periodic_fn: Optional[Callable[[], Any]] = None)[source]#
Bases:
object
Periodically checks whether a function shall be called as a result of an update, the function potentially being non-atomic (i.e. it may take a long time to execute such that new updates may come in while it is executing). Two function all mechanisms are in place:
a function which is called if there has not been a new update for a certain time period (which may be called several times if updates come in while the function is being executed)
a function which is called periodically
- Parameters:
check_interval_secs – the time period, in seconds, between checks
no_update_time_period_secs – the time period after which to execute noUpdateFn if no further updates have come in. This must be at least as large as checkIntervalSecs. If None, use checkIntervalSecs.
no_update_fn – the function to call if there have been no further updates for noUpdateTimePeriodSecs seconds
periodic_fn – a function to execute periodically (every checkIntervalSecs seconds) in the busy waiting loop, which may, for example, log information or apply additional executions, which must not interfere with the correctness of the execution of fn
- class PicklePersistentKeyValueCache(pickle_path, version=1, save_on_update=True, deferred_save_delay_secs=1.0)[source]#
Bases:
PersistentKeyValueCache
[TKey
,TValue
]Represents a key-value cache as a dictionary which is persisted in a file using pickle
- Parameters:
pickle_path – the path of the file where the cache values are to be persisted
version – the version of cache entries. If a persisted cache with a non-matching version is found, it is discarded
save_on_update – whether to persist the cache after an update; the cache is saved in a deferred manner and will be saved after deferredSaveDelaySecs if no new updates have arrived in the meantime, i.e. it will ultimately be saved deferredSaveDelaySecs after the latest update
deferred_save_delay_secs – the number of seconds to wait for additional data to be added to the cache before actually storing the cache after a cache update
- class SlicedPicklePersistentList(directory, pickle_base_name, num_entries_per_slice=100000)[source]#
Bases:
PersistentList
Object handling the creation and access to sliced pickle caches
- Parameters:
directory – path to the directory where the sliced caches are to be stored
pickle_base_name – base name for the pickle, where slices will have the names {pickleBaseName}_sliceX.pickle
num_entries_per_slice – how many entries should be stored in each cache
- class SqlitePersistentKeyValueCache(path, table_name='cache', deferred_commit_delay_secs=1.0, key_type: KeyType = KeyType.STRING, max_key_length=255)[source]#
Bases:
PersistentKeyValueCache
[TKey
,TValue
]- Parameters:
path – the path to the file that is to hold the SQLite database
table_name – the name of the table to create in the database
deferred_commit_delay_secs – the time frame during which no new data must be added for a pending transaction to be committed
key_type – the type to use for keys; for complex keys (i.e. tuples), use STRING (conversions to string are automatic)
max_key_length – the maximum key length for the case where the key_type can be parametrised (e.g. STRING)
- class KeyType(value)[source]#
Bases:
Enum
An enumeration.
- STRING = ('VARCHAR(%d)',)#
- INTEGER = ('LONG',)#
- set(key: TKey, value: TValue)[source]#
Sets a cached value
- Parameters:
key – the key under which to store the value
value – the value to store; since None is used indicate the absence of a value, None should not be used a value
- class SqlitePersistentList(path)[source]#
Bases:
PersistentList
- class CachedValueProviderMixin(cache: Optional[KeyValueCache[TKey, TValue]] = None, cache_factory: Optional[Callable[[], KeyValueCache[TKey, TValue]]] = None, persist_cache=False, box_values=False)[source]#
Bases:
Generic
[TKey
,TValue
,TData
],ABC
Represents a value provider that can provide values associated with (hashable) keys via a cache or, if cached values are not yet present, by computing them.
- Parameters:
cache – the cache to use or None. If None, caching will be disabled
cache_factory – a factory with which to create the cache (or recreate it after unpickling if persistCache is False, in which case this factory must be picklable)
persist_cache – whether to persist the cache when pickling
box_values – whether to box values, such that None is admissible as a value
- cached(fn: Callable[[], T], pickle_path, function_name=None, validity_check_fn: Optional[Callable[[T], bool]] = None, backend='pickle', protocol=5, load=True, version=None) T [source]#
Calls the given function unless its result is already cached (in a pickle), in which case it will read the cached result and return it.
Rather than directly calling this function, consider using the decorator variant
pickle_cached()
.- Parameters:
fn – the function whose result is to be cached
pickle_path – the path in which to store the cached result
function_name – the name of the function fn (for the case where its __name__ attribute is not informative)
validity_check_fn – an optional function to call in order to check whether a cached result is still valid; the function shall return True if the result is still valid and false otherwise. If a cached result is invalid, the function fn is called to compute the result and the cached result is updated.
backend – pickle or joblib
protocol – the pickle protocol version
load – whether to load a previously persisted result; if False, do not load an old result but store the newly computed result
version – if not None, previously persisted data will only be returned if it was stored with the same version
- Returns:
the result (either obtained from the cache or the function)
- pickle_cached(cache_base_path: str, filename_prefix: Optional[str] = None, filename: Optional[str] = None, backend='pickle', protocol=5, load=True, version=None)[source]#
Function decorator for caching function results via pickle.
Add this decorator to any function to cache its results in pickle files. The function may have arguments, in which case the cache will be specific to the actual arguments by computing a hash code from their pickled representation.
- Parameters:
cache_base_path – the directory where the pickle cache file will be stored
filename_prefix – a prefix of the name of the cache file to be created, to which the function name and, where applicable, a hash code of the function arguments as well as the extension “.cache.pickle” will be appended. The prefix need not end in a separator, as “-” will automatically be added between filename components.
filename – the full file name of the cache file to be created; if the function takes arguments, the filename must contain a placeholder ‘%s’ for the argument hash
backend – the serialisation backend to use (see dumpPickle)
protocol – the pickle protocol version to use
load – whether to load a previously persisted result; if False, do not load an old result but store the newly computed result
version – if not None, previously persisted data will only be returned if it was stored with the same version
- PickleCached(cache_base_path: str, filename_prefix: Optional[str] = None, filename: Optional[str] = None, backend='pickle', protocol=5, load=True, version=None)#
Function decorator for caching function results via pickle.
Add this decorator to any function to cache its results in pickle files. The function may have arguments, in which case the cache will be specific to the actual arguments by computing a hash code from their pickled representation.
- Parameters:
cache_base_path – the directory where the pickle cache file will be stored
filename_prefix – a prefix of the name of the cache file to be created, to which the function name and, where applicable, a hash code of the function arguments as well as the extension “.cache.pickle” will be appended. The prefix need not end in a separator, as “-” will automatically be added between filename components.
filename – the full file name of the cache file to be created; if the function takes arguments, the filename must contain a placeholder ‘%s’ for the argument hash
backend – the serialisation backend to use (see dumpPickle)
protocol – the pickle protocol version to use
load – whether to load a previously persisted result; if False, do not load an old result but store the newly computed result
version – if not None, previously persisted data will only be returned if it was stored with the same version
- class PickleLoadSaveMixin[source]#
Bases:
LoadSaveInterface