Skip to content

Commit

Permalink
add a bit of caching to API responses for performance (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
acannistra authored Nov 21, 2024
1 parent ef8fa00 commit 7b381a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions avalanche.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict
from typing import List
from os import path
from cachetools import func

from prometheus_client import Histogram

Expand All @@ -17,6 +18,9 @@ class APIException(Exception):
pass


FORECAST_CACHE_TTL = 60


class AvalancheAPI:
def __init__(self):
self.centers: Optional[Dict] = self.load_centers()
Expand Down Expand Up @@ -60,10 +64,12 @@ def get_zones(self, center: str) -> List:
except KeyError:
raise APIException(f"{center} not found.")

@func.ttl_cache(maxsize=50, ttl=FORECAST_CACHE_TTL)
def get_center_meta(self, center_id: str) -> Dict:
_url = API_BASE + f"/avalanche-center/{center_id}"
return requests.get(_url).json()

@func.ttl_cache(maxsize=50, ttl=FORECAST_CACHE_TTL)
def get_forecast(self, center_id: str, zone_id: str):
_url = (
API_BASE + f"/product?type=forecast&center_id={center_id}&zone_id={zone_id}"
Expand Down
4 changes: 3 additions & 1 deletion scripts/testzones.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@


def valid_fcst(fcst):
return fcst["bottom_line"] != None and len(fcst["danger"]) > 0
return fcst["hazard_discussion"] or (
fcst["bottom_line"] != None and len(fcst["danger"]) > 0
)


test_centers = [
Expand Down

0 comments on commit 7b381a2

Please sign in to comment.