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

Bugfix: API V2 cache management #3339

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.92.1
2.92.1+dev
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ CHANGELOG
2.92.1+dev (XXXX-XX-XX)
-----------------------

**Bug fixes**

- Fix cache management in API v2


2.92.1 (2022-12-01)
-----------------------
Expand Down
5 changes: 4 additions & 1 deletion geotrek/api/v2/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mapentity.renderers import GeoJSONRenderer
from rest_framework import viewsets, renderers
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated

from geotrek.api.v2 import pagination as api_pagination, filters as api_filters
Expand Down Expand Up @@ -37,7 +38,9 @@ def get_base_cache_string(self):
def get_object_cache_key(self, pk):
""" return specific object cache key based on object date_update column"""
# don't directly use get_object or get_queryset to avoid select / prefetch and annotation sql queries
date_update = self.get_queryset().model.objects.get(pk=pk).date_update
# insure object exists and doesn't raise exception
instance = get_object_or_404(self.get_queryset().model, pk=pk)
date_update = instance.date_update
return f"{self.get_base_cache_string()}:{date_update.isoformat()}"

def object_cache_key_func(self, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions geotrek/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ def api_bbox(bbox, buffer):
LEAFLET_CONFIG['SPATIAL_EXTENT'] = api_bbox(SPATIAL_EXTENT, VIEWPORT_MARGIN)

REST_FRAMEWORK_EXTENSIONS = {
'DEFAULT_USE_CACHE': 'api_v2',
'DEFAULT_CACHE_ERRORS': False
}

Expand Down