Framework for Python

Caching

cache_remember()

Return the cached value, or compute it, cache it, and return it.

Call it with a function that takes no arguments::

model = fw.cache_remember("model", lambda: fit(data), expire_after="1 week")

Usage

cache_remember(
    name: str,
    compute: Callable[[], T] | None = None,
    *,
    file: str | Path | None = None,
    expire_after: float | str | None = None,
    refresh: bool | Callable[[], bool] = False,
) -> T | Callable[[Callable[[], T]], Callable[[], T]]

Arguments

Name Type Default Description
name required str

The cache name.

compute Callable[[], T] | None None

Computes the value on a miss.

file str | Path | None None

Store the value in this file instead of the cache folder.

expire_after float | str | None None

Hours until the value expires, or a string like "2 days".

refresh bool | Callable[[], bool] False

Recompute even when cached. A function is called to decide.

Details

or use it as a decorator, which caches the function's result::

@fw.cache_remember("survey_clean")
def survey_clean():
    return fw.data_read("inputs.raw.survey").dropna()