meta: introduce a Cache decorator (MR 2252)

Generalise pmb.helpers.other.cache with a more python decorator.

The Cache decorator takes a list of function arguments to use as cache
keys, keyword args can be used to restrict caching so that it is skipped
entirely unless the attribute has a specific value.

For example, pmb.helpers.pmaports.get() has the decorator:
@Cache("pkgname", subpackages=True)

This means the return value will be cached only when subpackages is
True, otherwise it will always miss.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-06-09 13:22:58 +02:00 committed by Oliver Smith
parent 79cf2e8910
commit 185d8bcef5
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
20 changed files with 244 additions and 165 deletions

View file

@ -5,14 +5,11 @@ from typing import Any, Dict, Generator, List, Optional, Tuple
import pmb.config
from pmb.core.context import get_context
from pmb.meta import Cache
_cache: Dict[str, Any] = {"pkgrepo_paths": []}
@Cache(skip_extras=False)
def pkgrepo_paths(skip_extras = False) -> List[Path]:
global _cache
if not skip_extras and _cache["pkgrepo_paths"]:
return _cache["pkgrepo_paths"]
config = get_context().config
paths = list(map(lambda x: Path(x),
config.aports))
@ -29,7 +26,6 @@ def pkgrepo_paths(skip_extras = False) -> List[Path]:
out_paths.append(p / "extra-repos/systemd")
out_paths.append(p)
_cache["pkgrepo_paths"] = out_paths
return out_paths
def pkgrepo_default_path() -> Path: