Skip to content

Commit

Permalink
ref cache stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Mar 7, 2024
1 parent 3a16630 commit 945374f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
46 changes: 34 additions & 12 deletions dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ def _is_ref_lookup_subtypes(ref_lookup_flags: int) -> bool:
return (ref_lookup_flags & StructuredDataSet.REF_LOOKUP_SUBTYPES) == StructuredDataSet.REF_LOOKUP_SUBTYPES

@property
def ref_cache_hit_count(self) -> int:
return self.portal.ref_cache_hit_count if self.portal else -1
def ref_lookup_cache_hit_count(self) -> int:
return self.portal.ref_lookup_cache_hit_count if self.portal else -1

@property
def ref_lookup_count(self) -> int:
return self.portal.ref_lookup_count if self.portal else -1
def ref_lookup_cache_miss_count(self) -> int:
return self.portal.ref_lookup_cache_miss_count if self.portal else -1

@property
def ref_lookup_found_count(self) -> int:
Expand All @@ -303,6 +303,14 @@ def ref_lookup_notfound_count(self) -> int:
def ref_lookup_error_count(self) -> int:
return self.portal.ref_lookup_error_count if self.portal else -1

@property
def ref_exists_cache_hit_count(self) -> int:
return self.portal.ref_exists_cache_hit_count if self.portal else -1

@property
def ref_exists_cache_miss_count(self) -> int:
return self.portal.ref_exists_cache_miss_count if self.portal else -1

def _note_warning(self, item: Optional[Union[dict, List[dict]]], group: str) -> None:
self._note_issue(self._warnings, item, group)

Expand Down Expand Up @@ -705,19 +713,18 @@ def __init__(self,
else:
self.get_metadata = self.get_metadata_cache
self._ref_cache = {}
self._ref_cache_hit_count = 0
self._ref_lookup_count = 0
self._ref_lookup_found_count = 0
self._ref_lookup_notfound_count = 0
self._ref_lookup_error_count = 0
self._ref_exists_cache_hit_count = 0
self._ref_exists_cache_miss_count = 0

@lru_cache(maxsize=8092)
def get_metadata_cache(self, object_name: str) -> Optional[dict]:
return self.get_metadata_nocache(object_name)

def get_metadata_nocache(self, object_name: str) -> Optional[dict]:
try:
self._ref_lookup_count += 1
result = super().get_metadata(object_name)
self._ref_lookup_found_count += 1
return result
Expand Down Expand Up @@ -783,9 +790,10 @@ def ref_exists(self, type_name: str, value: Optional[str] = None) -> List[dict]:
else:
return [] # Should not happen.
if (resolved := self._ref_exists_from_cache(type_name, value)) is not None:
self._ref_cache_hit_count += 1
self._ref_exists_cache_hit_count += 1
return resolved
# Not cached here.
self._ref_exists_cache_miss_count += 1
resolved = []
ref_lookup_strategy = self._ref_lookup_strategy(type_name, value)
is_ref_lookup_root = StructuredDataSet._is_ref_lookup_root(ref_lookup_strategy)
Expand Down Expand Up @@ -827,12 +835,18 @@ def _ref_exists_single(self, type_name: str, value: str, root: bool = False) ->
return True, value.get("uuid")

@property
def ref_cache_hit_count(self) -> int:
return self._ref_cache_hit_count
def ref_lookup_cache_hit_count(self) -> int:
try:
return self.get_metadata_cache.cache_info().hits
except Exception:
return -1

@property
def ref_lookup_count(self) -> int:
return self._ref_lookup_count
def ref_lookup_cache_miss_count(self) -> int:
try:
return self.get_metadata_cache.cache_info().misses
except Exception:
return -1

@property
def ref_lookup_found_count(self) -> int:
Expand All @@ -846,6 +860,14 @@ def ref_lookup_notfound_count(self) -> int:
def ref_lookup_error_count(self) -> int:
return self._ref_lookup_error_count

@property
def ref_exists_cache_hit_count(self) -> int:
return self._ref_exists_cache_hit_count

@property
def ref_exists_cache_miss_count(self) -> int:
return self._ref_exists_cache_miss_count

@staticmethod
def create_for_testing(arg: Optional[Union[str, bool, List[dict], dict, Callable]] = None,
schemas: Optional[List[dict]] = None) -> Portal:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.8.0.1b2" # TODO: To become 8.8.1
version = "8.8.0.1b3" # TODO: To become 8.8.1
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <support@4dnucleome.org>"]
license = "MIT"
Expand Down

0 comments on commit 945374f

Please sign in to comment.