lair.records.Cacher#
- class lair.records.Cacher(func, cache_file, reload=False)[source]#
A class that caches function results to a file for future use.
A result is stored under a key made of the function’s code and the arguments of the call, so editing the function (or a function passed in the arguments) runs it again instead of returning the old result. Each parameter is also an attribute of the same name.
- Parameters:
func (function) – The function to be cached.
cache_file (str) – The name of the file to cache results to; it must end in
.pkl.reload (bool, optional) – Whether to re-run the function instead of returning cached results. Each set of arguments is re-run once per Cacher; the fresh result replaces the old one in the index and every other cached entry is kept. (The old result stays in
cache_filebut is no longer referenced.) Defaults to False.
Notes
What the key holds. The arguments are pickled. The code of
func, and of every function in the arguments (apreprocess=function, say), goes in as its compiled bytecode, its constants (a unit factor of 1000 changed to 100 is an edit), the names it uses, its default arguments and the functions it closes over (so a decorated function is the code of the function inside). A comment, a blank line, a renamed local variable and the function’s own docstring are not part of it: those edits keep the cached results. The key is the same in every process, on every run.What it does not see. The key reaches only as far as the function itself. An edit to a helper that the function calls, to a module-level value it reads, to a variable it closes over (unless that is a number, a string or a function), or to a file it reads leaves the key unchanged and returns the old result. Delete the cache file, or pass
reload=True, after such an edit.Rebuilds. A cache file written by an earlier lair has keys of another shape, so its results are not found: the function runs once for each set of arguments and the results are added to the file, behind the old ones, which stay in it. Bytecode differs between Python versions, so moving to another one (3.11 to 3.12) does the same.
Examples
>>> import os, tempfile >>> from lair.records import Cacher >>> def double(x): ... print("running") ... return 2 * x >>> with tempfile.TemporaryDirectory() as folder: ... cached = Cacher(double, os.path.join(folder, "double.pkl")) ... print(cached(4)) ... print(cached(4)) # not run again running 8 8
Methods
|
Return the cached result, or call the function and cache its result. |
Load the cache index from a file. |
|
Save the cache index to a file. |