| 118 | return Path(f"{prefix}{key.hash}") |
| 119 | |
| 120 | def cache_attempt( |
| 121 | self, |
| 122 | defs: set[Name], |
| 123 | key: HashKey, |
| 124 | stateful_refs: set[Name], |
| 125 | glbls: dict[str, Any] | None = None, |
| 126 | ) -> Cache: |
| 127 | start_time = time.time() |
| 128 | loaded = self.load_cache(key, glbls=glbls) |
| 129 | if not loaded: |
| 130 | return Cache.empty(defs=defs, key=key, stateful_refs=stateful_refs) |
| 131 | load_time = time.time() - start_time |
| 132 | |
| 133 | # TODO: Consider more robust verification |
| 134 | if loaded.hash != key.hash: |
| 135 | raise LoaderError("Hash mismatch in loaded cache.") |
| 136 | if (defs | stateful_refs) != set(loaded.defs): |
| 137 | raise LoaderError("Variable mismatch in loaded cache.") |
| 138 | self._hits += 1 |
| 139 | |
| 140 | # Track time savings: original runtime - time to load from cache |
| 141 | runtime = loaded.meta.get("runtime", 0) |
| 142 | if runtime > 0: |
| 143 | time_saved = runtime - load_time |
| 144 | self._time_saved += max(0, time_saved) |
| 145 | |
| 146 | return Cache.new( |
| 147 | loaded=loaded, |
| 148 | key=key, |
| 149 | stateful_refs=stateful_refs, |
| 150 | ) |
| 151 | |
| 152 | @property |
| 153 | def hits(self) -> int: |