Files

118 lines
3.4 KiB
Python
Raw Permalink Normal View History

2021-10-15 03:25:38 -05:00
"""
Handle loading of api-cache data.
"""
2021-07-10 10:12:35 -05:00
import json
import os
2018-11-08 19:45:21 +00:00
from django.conf import settings
class CacheRedirect(Exception):
"""
Raise this error to redirect to cache response during viewset.get_queryset
or viewset.list()
2021-10-15 03:25:38 -05:00
Argument should be an APICacheLoader instance.
2018-11-08 19:45:21 +00:00
"""
def __init__(self, loader):
2021-08-18 08:21:22 -05:00
super().__init__(self, "Result to be loaded from cache")
2018-11-08 19:45:21 +00:00
self.loader = loader
###############################################################################
# API CACHE LOADER
2020-07-15 02:07:01 -05:00
class APICacheLoader:
2018-11-08 19:45:21 +00:00
"""
Checks if an API GET request qualifies for a cache load
2021-10-15 03:25:38 -05:00
and if it does allows you to provide the cached result.
2018-11-08 19:45:21 +00:00
"""
def __init__(self, viewset, qset, filters):
request = viewset.request
self.request = request
self.qset = qset
self.filters = filters
self.model = viewset.model
self.viewset = viewset
self.depth = min(int(request.query_params.get("depth", 0)), 3)
self.limit = int(request.query_params.get("limit", 0))
self.skip = int(request.query_params.get("skip", 0))
self.since = int(request.query_params.get("since", 0))
self.fields = request.query_params.get("fields")
if self.fields:
self.fields = self.fields.split(",")
2019-12-05 16:57:52 +00:00
self.path = os.path.join(
2020-09-30 01:13:38 +00:00
settings.API_CACHE_ROOT,
f"{viewset.model.handleref.tag}-{self.depth}.json",
2019-12-05 16:57:52 +00:00
)
2018-11-08 19:45:21 +00:00
def qualifies(self):
"""
2021-10-15 03:25:38 -05:00
Check if request qualifies for a cache load.
2018-11-08 19:45:21 +00:00
"""
# api cache use is disabled, no
if not getattr(settings, "API_CACHE_ENABLED", False):
return False
# no depth and a limit lower than 251 seems like a tipping point
# were non-cache retrieval is faster still
2019-12-05 16:57:52 +00:00
if (
not self.depth
and self.limit
and self.limit <= 250
and getattr(settings, "API_CACHE_ALL_LIMITS", False) is False
):
2018-11-08 19:45:21 +00:00
return False
# filters have been specified, no
if self.filters or self.since:
return False
# cache file non-existant, no
if not os.path.exists(self.path):
return False
# request method is anything but GET, no
if self.request.method != "GET":
return False
# primary key set in request, no
if self.viewset.kwargs:
return False
return True
def load(self):
"""
2021-10-15 03:25:38 -05:00
Load the cached response according to tag and depth.
2018-11-08 19:45:21 +00:00
"""
# read cache file
2020-07-15 02:07:01 -05:00
with open(self.path) as f:
2018-11-08 19:45:21 +00:00
data = json.load(f)
data = data.get("data")
# apply pagination
if self.skip and self.limit:
2019-12-05 16:57:52 +00:00
data = data[self.skip : self.skip + self.limit]
2018-11-08 19:45:21 +00:00
elif self.skip:
2019-12-05 16:57:52 +00:00
data = data[self.skip :]
2018-11-08 19:45:21 +00:00
elif self.limit:
2019-12-05 16:57:52 +00:00
data = data[: self.limit]
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
if self.fields:
for row in data:
self.filter_fields(row)
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
return {"results": data, "__meta": {"generated": os.path.getmtime(self.path)}}
2018-11-08 19:45:21 +00:00
2021-01-13 20:35:07 +00:00
def filter_fields(self, row):
2018-11-08 19:45:21 +00:00
"""
2021-10-15 03:25:38 -05:00
Remove any unwanted fields from the resultset
according to the `fields` filter specified in the request.
2018-11-08 19:45:21 +00:00
"""
2021-01-13 20:35:07 +00:00
for field in list(row.keys()):
if field not in self.fields and field != "_grainy":
del row[field]