Caches and Storage
Caches
You can control how the results of a particular task type should be
formatted for caching by specifying an instance of one of the
following Cache classes for the cache
argument of the
labtech.task
decorator:
labtech.cache.PickleCache
Bases: BaseCache
Default cache that stores results as pickled Python objects.
NOTE: As pickle is not secure, you should only load pickle cache results that you trust.
Source code in labtech/cache.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
labtech.cache.NullCache
Bases: Cache
Cache that never stores results in the storage provider.
Source code in labtech/cache.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Custom Caches
You can define your own type of Cache with its own format or behaviour
by inheriting from BaseCache
:
labtech.cache.BaseCache
Bases: Cache
Base class for defining a Cache that will store results in a storage provider.
Source code in labtech/cache.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
KEY_PREFIX = ''
class-attribute
instance-attribute
Prefix for all files created by this Cache type - should be different for each Cache type to avoid conflicts.
save_result(storage: Storage, task: Task[ResultT], result: ResultT)
abstractmethod
Saves the given task result into the storage provider.
Parameters:
-
storage
(Storage
) –Storage provider to save the result into
-
task
(Task[ResultT]
) –task instance the result belongs to
-
result
(ResultT
) –result to save
Source code in labtech/cache.py
140 141 142 143 144 145 146 147 148 149 |
|
load_result(storage: Storage, task: Task[ResultT]) -> ResultT
abstractmethod
Loads the result for the given task from the storage provider.
Parameters:
-
storage
(Storage
) –Storage provider to load the result from
-
task
(Task[ResultT]
) –task instance to load the result for
Source code in labtech/cache.py
130 131 132 133 134 135 136 137 138 |
|
Storage
You can set the storage location for caching task results by
specifying an instance of one of the following Storage classes for the
storage
argument of your Lab
:
labtech.storage.LocalStorage
Bases: Storage
Storage provider that stores cached results in a local filesystem directory.
Source code in labtech/storage.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
__init__(storage_dir: Union[str, Path], *, with_gitignore: bool = True)
Parameters:
-
storage_dir
(Union[str, Path]
) –Path to the directory where cached results will be stored. The directory will be created if it does not already exist.
-
with_gitignore
(bool
, default:True
) –If
True
, a.gitignore
file will be created inside the storage directory to ignore the entire storage directory. If an existing.gitignore
file exists, it will be replaced.
Source code in labtech/storage.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
labtech.storage.NullStorage
Bases: Storage
Storage provider that does not store cached results.
Source code in labtech/storage.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Custom Storage
To store cached results with an alternative storage provider (such as
a storage bucket in the cloud), you can define your own type of
Storage by inheriting from Storage
:
labtech.storage.Storage
Bases: ABC
Storage provider for persisting cached task results.
Source code in labtech/types.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
find_keys() -> Sequence[str]
abstractmethod
Returns the keys of all currently cached task results.
Source code in labtech/types.py
109 110 111 |
|
exists(key: str) -> bool
abstractmethod
Returns True
if the given task key
is present in the storage
cache.
Source code in labtech/types.py
113 114 115 116 |
|
file_handle(key: str, filename: str, *, mode: str = 'r') -> IO
abstractmethod
Opens and returns a File-like object for a single file within the storage cache.
Parameters:
-
key
(str
) –The task key of the cached result containing the file.
-
filename
(str
) –The name of the file to open.
-
mode
(str
, default:'r'
) –The file mode to open the file with.
Source code in labtech/types.py
118 119 120 121 122 123 124 125 126 127 128 |
|
delete(key: str) -> None
abstractmethod
Deletes the cached result for the task with the given key
.
Source code in labtech/types.py
130 131 132 |
|