Skip to content

Commit 8c15f67

Browse files
authored
Merge pull request #276 from cmu-delphi/development
Added caching
2 parents d000207 + 8db15dc commit 8c15f67

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/indicatorsets/utils.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import requests
88
from django.conf import settings
9+
from django.core.cache import cache
910
from django.http import JsonResponse
1011
from epiweeks import Week
1112
from delphi_utils import get_structured_logger
@@ -814,14 +815,18 @@ def get_num_locations_from_meta(indicators):
814815
indicators = set(
815816
(indicator["source"], indicator["name"]) for indicator in indicators
816817
)
817-
try:
818-
metadata = requests.get(
819-
f"{settings.EPIDATA_URL}covidcast_meta/", timeout=5
820-
).json()["epidata"]
821-
for r in metadata:
822-
if (r["data_source"], r["signal"]) in indicators:
823-
timeseries_count += r["num_locations"]
824-
except Exception as e:
825-
print(f"Error fetching covidcast metadata: {e}")
826-
return 0
818+
metadata = cache.get("covidcast_meta")
819+
if metadata is None:
820+
try:
821+
metadata = requests.get(
822+
f"{settings.EPIDATA_URL}covidcast_meta/", timeout=5
823+
).json()["epidata"]
824+
# Cache for 24 hours
825+
cache.set("covidcast_meta", metadata, 60 * 60 * 24)
826+
except Exception as e:
827+
print(f"Error fetching covidcast metadata: {e}")
828+
return 0
829+
for r in metadata:
830+
if (r["data_source"], r["signal"]) in indicators:
831+
timeseries_count += r["num_locations"]
827832
return timeseries_count

src/indicatorsets/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ def get_context_data(self, **kwargs):
245245
),
246246
).order_by("beta_last", "-is_top_priority", "-delphi_hosted", "name")
247247
related_indicators = self.get_related_indicators(
248-
filter.indicators_qs, filter.qs.values_list("id", flat=True)
248+
filter.indicators_qs.select_related("indicator_set", "source"),
249+
filter.qs.values_list("id", flat=True),
249250
)
250251
context["related_indicators"] = json.dumps(related_indicators)
251252
context["num_of_timeseries"] = get_num_locations_from_meta(

0 commit comments

Comments
 (0)