Skip to content

Commit

Permalink
Merge pull request #58 from NASA-IMPACT/feature/57-cache
Browse files Browse the repository at this point in the history
Feature/57 cache
  • Loading branch information
drewbo authored Aug 25, 2020
2 parents 8f21411 + fdd27c8 commit 5ca3b57
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
48 changes: 41 additions & 7 deletions covid_api/api/api_v1/endpoints/datasets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Dataset endpoints."""

from covid_api.db.static.errors import InvalidIdentifier
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, Depends, Response

from covid_api.models.static import Datasets
from covid_api.db.static.datasets import datasets

from covid_api.db.memcache import CacheLayer
from covid_api.api import utils

router = APIRouter()

Expand All @@ -15,9 +15,25 @@
responses={200: dict(description="return a list of all available datasets")},
response_model=Datasets,
)
def get_datasets():
def get_datasets(
response: Response, cache_client: CacheLayer = Depends(utils.get_cache),
):
"""Return a list of datasets."""
return datasets.get_all()
dataset_hash = utils.get_hash(spotlight_id="all")
content = None

if cache_client:
try:
content = cache_client.get_image_from_cache(dataset_hash)
response.headers["X-Cache"] = "HIT"
except Exception:
content = None
if not content:
content = datasets.get_all()
if cache_client and content:
cache_client.set_dataset_cache(dataset_hash, content)

return content


@router.get(
Expand All @@ -27,10 +43,28 @@ def get_datasets():
},
response_model=Datasets,
)
def get_dataset(spotlight_id: str):
def get_dataset(
spotlight_id: str,
response: Response,
cache_client: CacheLayer = Depends(utils.get_cache),
):
"""Return dataset info for all datasets available for a given spotlight"""
try:
return datasets.get(spotlight_id)
dataset_hash = utils.get_hash(spotlight_id=spotlight_id)
content = None

if cache_client:
try:
content = cache_client.get_image_from_cache(dataset_hash)
response.headers["X-Cache"] = "HIT"
except Exception:
content = None
if not content:
content = datasets.get(spotlight_id)
if cache_client and content:
cache_client.set_dataset_cache(dataset_hash, content)

return content
except InvalidIdentifier:
raise HTTPException(
status_code=404, detail=f"Invalid spotlight identifier: {spotlight_id}"
Expand Down
2 changes: 1 addition & 1 deletion covid_api/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_cache(request: Request) -> CacheLayer:


def get_hash(**kwargs: Any) -> str:
"""Create hash from a dict."""
"""Create hash from kwargs."""
return hashlib.sha224(json.dumps(kwargs, sort_keys=True).encode()).hexdigest()


Expand Down
13 changes: 12 additions & 1 deletion covid_api/db/memcache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""covid_api.cache.memcache: memcached layer."""

from typing import Optional, Tuple
from typing import Optional, Tuple, Dict

from bmemcached import Client

Expand Down Expand Up @@ -61,3 +61,14 @@ def set_image_cache(
return self.client.set(img_hash, body, time=timeout)
except Exception:
return False

def get_dataset_from_cache(self, ds_hash: str) -> Dict:
"""Get dataset response from cache layer"""
return self.client.get(ds_hash)

def set_dataset_cache(self, ds_hash: str, body: Dict, timeout: int = 3600) -> bool:
"""Set dataset response in cache layer"""
try:
return self.client.set(ds_hash, body, time=timeout)
except Exception:
return False

0 comments on commit 5ca3b57

Please sign in to comment.