Skip to content

Commit b0348be

Browse files
add convenience loader and remove workflow type from cache key
1 parent 1cd8a82 commit b0348be

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

tidy3d/web/api/container.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ def load_sim_data(self, task_name: str) -> WorkflowDataType:
583583
task_data_path = self.task_paths[task_name]
584584
task_id = self.task_ids[task_name]
585585
from_cache = self.cached_tasks[task_name] if self.cached_tasks else False
586-
web.get_info(task_id)
586+
if not from_cache:
587+
web.get_info(task_id)
587588

588589
return web.load(
589590
task_id=task_id,

tidy3d/web/api/webapi.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _task_dict_to_url_bullet_list(data_dict: dict) -> str:
124124
return "\n".join([f"- {key}: '{value}'" for key, value in data_dict.items()])
125125

126126

127-
def _get_simulation_data_from_cache_entry(entry: CacheEntry, path: str) -> bool:
127+
def _copy_simulation_data_from_cache_entry(entry: CacheEntry, path: str) -> bool:
128128
if entry is not None:
129129
try:
130130
entry.materialize(Path(path))
@@ -137,18 +137,39 @@ def _get_simulation_data_from_cache_entry(entry: CacheEntry, path: str) -> bool:
137137
def restore_simulation_if_cached(
138138
simulation: WorkflowType,
139139
path: str,
140-
use_cache: Optional[bool],
141-
reduce_simulation: Literal["auto", True, False],
140+
use_cache: Optional[bool] = None,
141+
reduce_simulation: Literal["auto", True, False] = "auto",
142142
) -> bool:
143143
simulation_cache = resolve_simulation_cache(use_cache)
144-
loaded_from_cache = False
144+
copied_from_cache = False
145145
if simulation_cache is not None:
146146
sim_for_cache = simulation
147147
if isinstance(simulation, (ModeSolver, ModeSimulation)):
148148
sim_for_cache = get_reduced_simulation(simulation, reduce_simulation)
149149
entry = simulation_cache.try_fetch(simulation=sim_for_cache)
150-
loaded_from_cache = _get_simulation_data_from_cache_entry(entry, path)
151-
return loaded_from_cache
150+
if entry is not None:
151+
copied_from_cache = _copy_simulation_data_from_cache_entry(entry, path)
152+
return copied_from_cache
153+
154+
155+
def load_simulation_if_cached(
156+
simulation: WorkflowType,
157+
path: str,
158+
use_cache: Optional[bool] = None,
159+
reduce_simulation: Literal["auto", True, False] = "auto",
160+
) -> Optional[WorkflowDataType]:
161+
restored = restore_simulation_if_cached(simulation, path, use_cache, reduce_simulation)
162+
if restored:
163+
data = load(
164+
task_id=None,
165+
path=path,
166+
from_cache=True,
167+
)
168+
if isinstance(simulation, ModeSolver):
169+
simulation._patch_data(data=data)
170+
return data
171+
else:
172+
return None
152173

153174

154175
@wait_for_connection
@@ -256,11 +277,11 @@ def run(
256277
:meth:`tidy3d.web.api.container.Batch.monitor`
257278
Monitor progress of each of the running tasks.
258279
"""
259-
loaded_from_cache = restore_simulation_if_cached(
280+
copied_from_cache = restore_simulation_if_cached(
260281
simulation=simulation, path=path, use_cache=use_cache, reduce_simulation=reduce_simulation
261282
)
262283

263-
if not loaded_from_cache:
284+
if not copied_from_cache:
264285
task_id = upload(
265286
simulation=simulation,
266287
task_name=task_name,
@@ -291,7 +312,7 @@ def run(
291312
verbose=verbose,
292313
progress_callback=progress_callback_download,
293314
use_cache=use_cache,
294-
from_cache=loaded_from_cache,
315+
from_cache=copied_from_cache,
295316
lazy=lazy,
296317
)
297318

tidy3d/web/cache.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ def try_fetch(
500500

501501
cache_key = build_cache_key(
502502
simulation_hash=simulation_hash,
503-
workflow_type=workflow_type,
504503
version=versions,
505504
)
506505

@@ -538,7 +537,6 @@ def store_result(
538537

539538
cache_key = build_cache_key(
540539
simulation_hash=simulation_hash,
541-
workflow_type=workflow_type,
542540
version=version,
543541
)
544542

@@ -658,14 +656,12 @@ def _canonicalize(value: Any) -> Any:
658656
def build_cache_key(
659657
*,
660658
simulation_hash: str,
661-
workflow_type: str,
662659
version: str,
663660
) -> str:
664661
"""Construct a deterministic cache key."""
665662

666663
payload = {
667664
"simulation_hash": simulation_hash,
668-
"workflow_type": workflow_type,
669665
"versions": _canonicalize(version),
670666
}
671667
encoded = json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8")

0 commit comments

Comments
 (0)