lisa.utils.DirCache#
- class lisa.utils.DirCache(category, populate=None, fmt_version=None)[source]#
Bases:
Loggable
Provide a folder-based cache.
- Parameters:
category (str) – Unique name for the cache category. This allows an arbitrary number of categories to be used under
lisa.utils.LISA_CACHE_HOME
.populate (collections.abc.Callable) –
Callback to populate a new cache entry if none is found. It will be passed the following parameters:
The key that is being looked up
The path to populate
It must return a subfolder of the passed path to populate, or
None
, which is the same as returning the passed path.fmt_version (str or None) – Version of the format of this cache
category
. This allows re-using the cache across multiple versions oflisa
, at the expense of having to manually bump the format version in source code when the format of the cache changes. This format version is logically added to each key lookup so that multiple versions oflisa
do not interfere with each other.
The cache is managed in a process-safe way, so that there can be no race between concurrent processes or threads.
Properties
logger
inheritedConvenience short-hand for
self.get_logger()
.Methods
Return the folder of a cache entry.
Return the token associated with the given
key
.Check if the given
key
is already present in the cache. If the key is present, return the path, otherwise returnsNone
.get_logger()
inheritedProvides a
logging.Logger
named aftercls
.log_locals()
inheritedDebugging aid: log the local variables of the calling function.
Properties#
- property DirCache.logger#
Inherited property, see
lisa.utils.Loggable.logger
Convenience short-hand for
self.get_logger()
.
Methods#
- DirCache.get_entry(key)[source]#
Return the folder of a cache entry.
If no entry is found, a new one is created using the
populate()
callback.- Parameters:
key (object) –
Key of the cache entry. All the components of the key must be isomorphic to their
repr()
, otherwise the cache will be hit in cases where it should not. For convenience, some types are normalized:Mapping
is only considered for its keys and values and type name. Keys are sorted are sorted. If the passed object contains other relevant metadata, it should be rendered to a string first by the caller.Iterable
keys are normalized and the object is only considered as an iterable. If other relevant metadata is contained in the object, it should be rendered to a string by the caller.
Note
The return folder must never be modified, as it would lead to races.
- DirCache.has_key(key)[source]#
Check if the given
key
is already present in the cache. If the key is present, return the path, otherwise returnsNone
.- Parameters:
key – Same as for
get_entry()
.
- classmethod DirCache.get_logger(suffix=None)#
Inherited method, see
lisa.utils.Loggable.get_logger()
Provides a
logging.Logger
named aftercls
.
- classmethod DirCache.log_locals(var_names=None, level='debug')#
Inherited method, see
lisa.utils.Loggable.log_locals()
Debugging aid: log the local variables of the calling function.