stouputils.decorators.simple_cache module#
- ALL_CACHES: list[dict[Any, Any]] = [{"('Stoupy51', 'stouputils', 2){}": ['latest', '1.29.3', '1.29.2', '1.29.1', '1.28.1', '1.28.0', '1.27.1', '1.26.4', '1.25.0', '1.24.13', '1.23.2', '1.22.3', '1.21.4', '1.20.4', '1.19.5', '1.18.6', '1.17.0', '1.16.3', '1.15.1', '1.14.3']}][source]#
Registry of every cache dict created by
simple_cache(). Callclear_simple_caches()to clear all of them at once.
- clear_simple_caches() None[source]#
Clear every cache created by
simple_cache().Useful for long-lived processes that run the same code on changing state: call this at the start of each cycle so cached results (and skipped side effects) from a previous cycle can’t leak into the next one.
Examples
>>> @simple_cache ... def count_calls(x: int, _calls: list[int] = []) -> int: ... _calls.append(x) ... return len(_calls) >>> count_calls(1), count_calls(1) (1, 1) >>> clear_simple_caches() >>> count_calls(1) 2
- simple_cache(
- func: Callable[..., T],
- *,
- method: Literal['str', 'pickle'] | Callable[[tuple[Any, ...], dict[str, Any]], Any] = 'str',
- simple_cache(
- func: None = None,
- *,
- method: Literal['str', 'pickle'] | Callable[[tuple[Any, ...], dict[str, Any]], Any] = 'str',
Decorator that caches the result of a function based on its arguments.
The str method is often faster than the pickle method (by a little) but not as accurate with complex objects.
- Parameters:
func (Callable[..., T] | None) – Function to cache
method (Literal["str", "pickle"]) – The method to use for caching.
Examples
>>> @simple_cache ... def test1(a: int, b: int) -> int: ... return a + b
>>> @simple_cache(method="str") ... def test2(a: int, b: int) -> int: ... return a + b >>> test2(1, 2) 3 >>> test2(1, 2) 3 >>> test2(3, 4) 7
Cache a recursive function: >>> @simple_cache … def factorial(n: int) -> int: … return n * factorial(n - 1) if n else 1 >>> factorial(10) # no previously cached result, makes 11 recursive calls 3628800 >>> factorial(5) # no new calls, just returns the cached result 120 >>> factorial(12) # two new recursive calls, factorial(10) is cached 479001600
Prevent a function from running more than once regardless of arguments: >>> @simple_cache(method=lambda x, y: 1) … def execute_one_time() -> None: … print(“Executed!”) >>> _ = [execute_one_time() for _ in range(3)] Executed!