Skip to content

Commit

Permalink
refactor: use django caching api instead of cache_page method
Browse files Browse the repository at this point in the history
  • Loading branch information
jkgeo committed Jan 24, 2025
1 parent 8550102 commit 55d36fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
27 changes: 12 additions & 15 deletions glam_api/glam/views/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from rio_tiler.io import COGReader

import rasterio
from rasterio import features

from rest_framework import viewsets
from rest_framework.response import Response
Expand All @@ -15,9 +14,7 @@
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
from django.core.cache import cache
from django.shortcuts import get_object_or_404
from django.conf import settings

Expand All @@ -29,7 +26,6 @@
BoundaryFeature,
BoundaryLayer,
AnomalyBaselineRaster,
DataSource,
)
from ..serializers import (
FeatureBodySerializer,
Expand Down Expand Up @@ -394,7 +390,6 @@ def query_custom_feature(self, request):
diff_year_param,
],
)
# @method_decorator(cache_page(86400 * 365)) # 1 year
def query_boundary_feature(
self,
request,
Expand All @@ -418,6 +413,13 @@ def query_boundary_feature(
anomaly_type = data.get("anomaly_type", None)
diff_year = data.get("diff_year", None)

if settings.USE_CACHING:
cache_key = f"boundary-query-{product_id}-{date}-{cropmask_id}-{layer_id}-{feature_id}-{baseline}-{baseline_type}-{anomaly}-{anomaly_type}-{diff_year}"

data = cache.get(cache_key)
if data:
return Response(data)

product_queryset = ProductRaster.objects.filter(product__product_id=product_id)

product_dataset = get_object_or_404(product_queryset, date=date)
Expand Down Expand Up @@ -463,9 +465,6 @@ def query_boundary_feature(
Decimal(str(data.std()))
* Decimal(str(product_dataset.product.variable.scale))
)
logging.info(
f"product mean: {mean}, min: {_min}, max: {_max}, stdev: {stdev}"
)

if cropmask_id != "no-mask":
mask_queryset = CropmaskRaster.objects.all()
Expand Down Expand Up @@ -506,9 +505,7 @@ def query_boundary_feature(
)

if baseline_type or anomaly_type:
logging.info(
f"baseline_type: {baseline_type}, anomaly_type: {anomaly_type}"
)

if anomaly_type:
baseline_type = anomaly_type if anomaly_type else "mean"
baseline = anomaly if anomaly else "5year"
Expand Down Expand Up @@ -589,9 +586,6 @@ def query_boundary_feature(
Decimal(str(stdev))
* Decimal(str(product_dataset.product.variable.scale))
)
logging.info(
f"baseline mean: {mean}, min: {_min}, max: {_max}, stdev: {stdev}"
)

if type(mean) != np.ma.core.MaskedConstant:
result = {"min": _min, "max": _max, "mean": mean, "std": stdev}
Expand All @@ -600,4 +594,7 @@ def query_boundary_feature(
except InvalidOperation:
result = {"value": "No Data"}

if settings.USE_CACHING:
cache.set(cache_key, result, timeout=(60 * 60 * 24 * 30))

return Response(result)
14 changes: 11 additions & 3 deletions glam_api/glam/views/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie, vary_on_headers
from django.core.cache import cache

from ..serializers import TilesSerializer
from ..renderers import PNGRenderer
Expand Down Expand Up @@ -220,7 +219,6 @@ class Tiles(viewsets.ViewSet):
],
operation_id="retrieve tile",
)
@method_decorator(cache_page(60 * 60 * 24 * 30)) # 30 days
def retrieve(
self,
request,
Expand Down Expand Up @@ -260,6 +258,13 @@ def retrieve(
stretch_max = data.get("stretch_max", None)
tile_size = data.get("tile_size", None)

if settings.USE_CACHING:
cache_key = f"tile-{product_id}-{date}-{z}-{x}-{y}-{cropmask_id}-{cropmask_threshold}-{anomaly}-{anomaly_type}-{diff_year}-{colormap}-{stretch_min}-{stretch_max}-{tile_size}"

data = cache.get(cache_key)
if data:
return Response(data)

if tile_size is None:
tile_size = settings.DEFAULT_TILE_SIZE

Expand Down Expand Up @@ -433,6 +438,9 @@ def retrieve(
add_mask=True,
)

if settings.USE_CACHING:
cache.set(cache_key, tile, timeout=(60 * 60 * 24 * 30))

return Response(tile)

@swagger_auto_schema(
Expand Down

0 comments on commit 55d36fd

Please sign in to comment.