Debian Codegraph @

test/lib/ansible_test/_internal/cache.py

raw
1 """Cache for commonly shared data that is intended to be immutable."""
2
3 from __future__ import annotations
4
5 import collections.abc as c
6
7 from .config import (
8 CommonConfig,
9 )
10
11
12 class CommonCache:
13 """Common cache."""
14
15 def __init__(self, args: CommonConfig) -> None:
16 self.args = args
17
18 def get[TValue](self, key: str, factory: c.Callable[[], TValue]) -> TValue:
19 """Return the value from the cache identified by the given key, using the specified factory method if it is not found."""
20 if key not in self.args.cache:
21 self.args.cache[key] = factory()
22
23 return self.args.cache[key]
24
25 def get_with_args[TValue](self, key: str, factory: c.Callable[[CommonConfig], TValue]) -> TValue:
26 """Return the value from the cache identified by the given key, using the specified factory method (which accepts args) if it is not found."""
27 if key not in self.args.cache:
28 self.args.cache[key] = factory(self.args)
29
30 return self.args.cache[key]