|
6 | 6 | from loguru import logger |
7 | 7 |
|
8 | 8 | from bfabric.experimental import MultiQuery |
9 | | -from bfabric.experimental.entity_lookup_cache import EntityLookupCache |
| 9 | +from bfabric.experimental.cache.context import get_cache_stack |
10 | 10 |
|
11 | 11 | if TYPE_CHECKING: |
12 | 12 | from pathlib import Path |
@@ -47,41 +47,41 @@ def _client(self) -> Bfabric | None: |
47 | 47 | @classmethod |
48 | 48 | def find(cls, id: int, client: Bfabric) -> Self | None: |
49 | 49 | """Finds an entity by its ID, if it does not exist `None` is returned.""" |
50 | | - cache = EntityLookupCache.instance() |
51 | | - if cache and cache.contains(entity_type=cls, entity_id=id): |
52 | | - return cache.get(entity_type=cls, entity_id=id) |
53 | | - else: |
54 | | - result = client.read(cls.ENDPOINT, obj={"id": int(id)}) |
55 | | - entity = cls(result[0], client=client) if len(result) == 1 else None |
56 | | - if cache: |
57 | | - cache.put(entity_type=cls, entity_id=id, entity=entity) |
58 | | - return entity |
| 50 | + cache_stack = get_cache_stack() |
| 51 | + cache_entry = cache_stack.item_get(entity_type=cls, entity_id=id) |
| 52 | + if cache_entry: |
| 53 | + return cache_entry |
| 54 | + |
| 55 | + result = client.read(cls.ENDPOINT, obj={"id": int(id)}) |
| 56 | + entity = cls(result[0], client=client) if len(result) == 1 else None |
| 57 | + cache_stack.item_put(entity_type=cls, entity_id=id, entity=entity) |
| 58 | + return entity |
59 | 59 |
|
60 | 60 | @classmethod |
61 | 61 | def find_all(cls, ids: list[int], client: Bfabric) -> dict[int, Self]: |
62 | 62 | """Returns a dictionary of entities with the given IDs. The order will generally match the input, however, |
63 | 63 | if some entities are not found they will be omitted and a warning will be logged. |
64 | 64 | """ |
65 | | - cache = EntityLookupCache.instance() |
| 65 | + cache_stack = get_cache_stack() |
66 | 66 | ids_requested = cls.__check_ids_list(ids) |
67 | 67 |
|
68 | 68 | # retrieve entities from cache and from B-Fabric as needed |
69 | | - results_cached = cache.get_all(entity_type=cls, entity_ids=ids) if cache else {} |
| 69 | + results_cached = cache_stack.item_get_all(entity_type=cls, entity_ids=ids) |
70 | 70 | results_fresh = cls.__retrieve_entities( |
71 | 71 | client=client, ids_requested=ids_requested, ids_cached=results_cached.keys() |
72 | 72 | ) |
73 | 73 |
|
74 | | - if cache: |
75 | | - for entity_id, entity in results_fresh.items(): |
76 | | - cache.put(entity_type=cls, entity_id=entity_id, entity=entity) |
77 | | - |
| 74 | + cache_stack.item_put_all(entity_type=cls, entities=results_fresh) |
78 | 75 | return cls.__ensure_results_order(ids_requested, results_cached, results_fresh) |
79 | 76 |
|
80 | 77 | @classmethod |
81 | 78 | def find_by(cls, obj: dict[str, Any], client: Bfabric, max_results: int | None = 100) -> dict[int, Self]: |
82 | 79 | """Returns a dictionary of entities that match the given query.""" |
83 | 80 | result = client.read(cls.ENDPOINT, obj=obj, max_results=max_results) |
84 | | - return {x["id"]: cls(x, client=client) for x in result} |
| 81 | + cache_stack = get_cache_stack() |
| 82 | + entities = {x["id"]: cls(x, client=client) for x in result} |
| 83 | + cache_stack.item_put_all(entity_type=cls, entities=entities) |
| 84 | + return entities |
85 | 85 |
|
86 | 86 | def dump_yaml(self, path: Path) -> None: |
87 | 87 | """Writes the entity's data dictionary to a YAML file.""" |
|
0 commit comments