Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BiG-CZ: Cache CUAHSI Requests #2260

Merged
merged 2 commits into from
Sep 20, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/mmw/apps/bigcz/clients/cuahsi/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from rest_framework.exceptions import ValidationError
from django.contrib.gis.geos import Point

from django.core.cache import cache
from django.conf import settings

from apps.bigcz.models import ResourceLink, ResourceList, BBox
Expand Down Expand Up @@ -193,9 +194,16 @@ def group_series_by_location(series):
return records


def make_request(request, **kwargs):
def make_request(request, expiry, **kwargs):
key = 'bigcz_{}_{}'.format(request.method.name,
hash(frozenset(kwargs.items())))
cached = cache.get(key)
if cached:
return cached

try:
response = recursive_asdict(request(**kwargs))
cache.set(key, response, timeout=expiry)
return response
except URLError, e:
if isinstance(e.reason, timeout):
Expand All @@ -208,6 +216,7 @@ def make_request(request, **kwargs):

def get_services_in_box(box):
result = make_request(client.service.GetServicesInBox2,
604800, # Cache for one week
xmin=box.xmin,
xmax=box.xmax,
ymin=box.ymin,
Expand All @@ -228,6 +237,7 @@ def get_series_catalog_in_box(box, from_date, to_date, networkIDs):
to_date = to_date or DATE_MAX

result = make_request(client.service.GetSeriesCatalogForBox2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you speculate on the size of these responses so we have an idea of the impact to the cache storage we have available?

Copy link
Member Author

@rajadain rajadain Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vagrant@services:~$ redis-cli -n 1 GET ":1:bigcz_GetServicesInBox2_-536472219463548920" > GetServicesInBox2.txt
vagrant@services:~$ redis-cli -n 1 GET ":1:bigcz_GetSeriesCatalogForBox2_6025281192626754923" > GetSeriesCatalogForBox2.txt
vagrant@services:~$ du -sh *.txt
392K	GetSeriesCatalogForBox2.txt  # Cached for 5 minutes
144K	GetServicesInBox2.txt        # Cached for 1 week

This is for the Philadelphia HUC-12 for a search query of "water" with 214 results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that the original issue only asked to cache GetServicesInBox2. I'm also caching the other one to make interacting with it faster.

300, # Cache for 5 minutes
xmin=box.xmin,
xmax=box.xmax,
ymin=box.ymin,
Expand Down