Skip to content

Commit

Permalink
add optional caching with joblib
Browse files Browse the repository at this point in the history
  • Loading branch information
missing-user committed Jan 8, 2025
1 parent c52dd99 commit b75c1de
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/simsopt/configs/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

try:
import requests

try:
import joblib
memory = joblib.Memory('./quasr_request_cache', verbose=0)
except ImportError:
memory = None
except ImportError:
requests = None
memory = None

from pathlib import Path
THIS_DIR = (Path(__file__).parent).resolve()
Expand Down Expand Up @@ -223,8 +230,16 @@ def get_QUASR_data(ID, return_style='quasr-style'):
id_str = f"{ID:07d}"
# string to 7 digits
url = f'https://quasr.flatironinstitute.org/simsopt_serials/{id_str[0:4]}/serial{id_str}.json'

with requests.get(url) as r:

if memory:
requests_get = memory.cache(requests.get)
else:
requests_get = requests.get
if not hasattr(get_QUASR_data, "cache_warning_issued"):
print("Warning: Caching of requests to the QUASR database is available if joblib is installed.")
get_QUASR_data.cache_warning_issued = True

with requests_get(url) as r:
if r.status_code == 200:
print(f"Configuration with ID {ID:07} downloaded successfully")
surfaces, coils = json.loads(r.content, cls=GSONDecoder)
Expand Down

0 comments on commit b75c1de

Please sign in to comment.