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

Feature/57 cache #58

Merged
merged 6 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
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) -> Dict:
drewbo marked this conversation as resolved.
Show resolved Hide resolved
"""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