|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING, Any |
4 | 4 |
|
| 5 | +from cachetools import LRUCache |
5 | 6 | from infrahub_sdk.schema import BranchSchema as SDKBranchSchema |
6 | 7 |
|
7 | 8 | from infrahub import lock |
@@ -42,7 +43,8 @@ class SchemaManager(NodeManager): |
42 | 43 | def __init__(self) -> None: |
43 | 44 | self._cache: dict[int, Any] = {} |
44 | 45 | self._branches: dict[str, SchemaBranch] = {} |
45 | | - self._sdk_branches: dict[str, SDKBranchSchema] = {} |
| 46 | + self._branch_hash_by_name: dict[str, str] = {} |
| 47 | + self._sdk_branches: LRUCache[str, SDKBranchSchema] = LRUCache(maxsize=10) |
46 | 48 |
|
47 | 49 | def _get_from_cache(self, key: int) -> Any: |
48 | 50 | return self._cache[key] |
@@ -147,12 +149,19 @@ def get_schema_branch(self, name: str) -> SchemaBranch: |
147 | 149 | return self._branches[name] |
148 | 150 |
|
149 | 151 | def get_sdk_schema_branch(self, name: str) -> SDKBranchSchema: |
150 | | - return self._sdk_branches[name] |
| 152 | + schema_hash = self._branch_hash_by_name[name] |
| 153 | + branch_schema = self._sdk_branches.get(schema_hash) |
| 154 | + if not branch_schema: |
| 155 | + self._sdk_branches[schema_hash] = SDKBranchSchema.from_api_response( |
| 156 | + data=self._branches[name].to_dict_api_schema_object() |
| 157 | + ) |
| 158 | + |
| 159 | + return self._sdk_branches[schema_hash] |
151 | 160 |
|
152 | 161 | def set_schema_branch(self, name: str, schema: SchemaBranch) -> None: |
153 | 162 | schema.name = name |
154 | 163 | self._branches[name] = schema |
155 | | - self._sdk_branches[name] = SDKBranchSchema.from_api_response(data=schema.to_dict_api_schema_object()) |
| 164 | + self._branch_hash_by_name[name] = schema.get_hash() |
156 | 165 |
|
157 | 166 | def process_schema_branch(self, name: str) -> None: |
158 | 167 | schema_branch = self.get_schema_branch(name=name) |
@@ -771,8 +780,8 @@ def purge_inactive_branches(self, active_branches: list[str]) -> list[str]: |
771 | 780 | for branch_name in list(self._branches.keys()): |
772 | 781 | if branch_name not in active_branches: |
773 | 782 | del self._branches[branch_name] |
774 | | - if branch_name in self._sdk_branches: |
775 | | - del self._sdk_branches[branch_name] |
| 783 | + if branch_name in self._branch_hash_by_name: |
| 784 | + del self._branch_hash_by_name[branch_name] |
776 | 785 | removed_branches.append(branch_name) |
777 | 786 |
|
778 | 787 | for hash_key in list(self._cache.keys()): |
|
0 commit comments