Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Refactor the cache handling for the exercise overview

Roland Geider40b425f6f205
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 27d0bd655..4baf07a91 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -18,6 +18,7 @@ from uuid import UUID
# Django
from django.conf import settings
+from django.core.cache import cache
from django.db.models import Prefetch
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
@@ -63,6 +64,7 @@ from wger.exercises.models import (
Translation,
)
from wger.exercises.views.helper import StreamVerbs
+from wger.utils.cache import CacheKeyMapper
class ExerciseViewSet(ModelViewSet):
@@ -226,14 +228,11 @@ class ExerciseInfoViewset(viewsets.ReadOnlyModelViewSet):
def get_queryset(self):
"""
- Optimize the queryset with select_related and prefetch_related to avoid
- n+1 queries
+ Heavy queryset with select_related/prefetch_related to avoid n+1 queries.
- One improvement is that we access the historical records of the exercise
- from the django-simple-history package, which are not really prefetchable
- since they are a manager.
+ Used by retrieve() and the cache-miss path in list(); warm list() requests
+ skip it entirely (see list()).
"""
-
return Exercise.objects.select_related(
'category',
'license',
@@ -249,6 +248,34 @@ class ExerciseInfoViewset(viewsets.ReadOnlyModelViewSet):
),
)
+ def list(self, request, *args, **kwargs):
+ """Serve the list from the per-exercise cache, hitting the DB only for misses."""
+
+ # Only id + uuid are needed to build the cache keys: filtering/ordering
+ # still apply, but no related rows are pulled.
+ queryset = self.filter_queryset(Exercise.objects.only('id', 'uuid'))
+ page = self.paginate_queryset(queryset)
+ exercises = page if page is not None else list(queryset)
+
+ keys = {
+ exercise.uuid: CacheKeyMapper.get_exercise_api_key(exercise.uuid)
+ for exercise in exercises
+ }
+ representations = cache.get_many(keys.values())
+
+ missing = [uuid for uuid, key in keys.items() if key not in representations]
+ if missing:
+ # to_representation() repopulates the cache as a side effect
+ for exercise in self.get_queryset().filter(uuid__in=missing):
+ representations[keys[exercise.uuid]] = self.get_serializer(exercise).data
+
+ data = [
+ representations[keys[exercise.uuid]]
+ for exercise in exercises
+ if keys[exercise.uuid] in representations
+ ]
+ return self.get_paginated_response(data) if page is not None else Response(data)
+
class ExerciseSubmissionViewSet(CreateAPIView):
"""
diff --git a/wger/exercises/tests/api/test_exercise_api_cache.py b/wger/exercises/tests/api/test_exercise_api_cache.py
index c86a9e830..51e534777 100644
--- a/wger/exercises/tests/api/test_exercise_api_cache.py
+++ b/wger/exercises/tests/api/test_exercise_api_cache.py
@@ -14,6 +14,9 @@
# Django
from django.core.cache import cache
+from django.db import connection
+from django.test.utils import CaptureQueriesContext
+from django.urls import reverse
# wger
from wger.core.tests.base_testcase import WgerTestCase
@@ -21,6 +24,7 @@ from wger.exercises.models import (
Alias,
Exercise,
ExerciseComment,
+ ExerciseImage,
Translation,
)
from wger.utils.cache import cache_mapper
@@ -144,3 +148,49 @@ class ExerciseApiCacheTestCase(WgerTestCase):
alias.delete()
self.assertFalse(cache.get(self.cache_key))
+
+
+class ExerciseInfoListCacheTestCase(WgerTestCase):
+ """
+ Tests the bulk cache handling of the exerciseinfo list endpoint
+ """
+
+ def test_warm_list_skips_prefetch_joins(self):
+ """
+ A warm list request reads the cached representations in bulk and must not
+ query the related tables that the heavy queryset would otherwise prefetch.
+ """
+ list_url = reverse('exerciseinfo-list')
+
+ # Warm the cache for every exercise
+ self.client.get(list_url, {'limit': 900})
+
+ related_tables = (
+ Translation._meta.db_table,
+ Alias._meta.db_table,
+ ExerciseComment._meta.db_table,
+ ExerciseImage._meta.db_table,
+ )
+
+ with CaptureQueriesContext(connection) as context:
+ self.client.get(list_url, {'limit': 900})
+
+ executed = ' '.join(query['sql'] for query in context.captured_queries)
+ for table in related_tables:
+ self.assertNotIn(table, executed)
+
+ def test_list_reuses_per_exercise_cache_from_retrieve(self):
+ """
+ retrieve() and list() share the same per-exercise cache entry, so an
+ exercise warmed through the detail endpoint is reused by the list.
+ """
+ exercise = Exercise.objects.get(pk=1)
+ key = cache_mapper.get_exercise_api_key(exercise.uuid)
+
+ self.assertFalse(cache.get(key))
+ detail = self.client.get(reverse('exerciseinfo-detail', kwargs={'pk': exercise.pk})).json()
+ self.assertTrue(cache.get(key))
+
+ results = self.client.get(reverse('exerciseinfo-list'), {'limit': 900}).json()['results']
+ listed = next(row for row in results if row['id'] == exercise.pk)
+ self.assertEqual(listed, detail)
diff --git a/wger/utils/models.py b/wger/utils/models.py
index 1648dab28..6e0cf2b97 100644
--- a/wger/utils/models.py
+++ b/wger/utils/models.py
@@ -166,10 +166,9 @@ def collect_model_author_history(model):
"""
Get unique set of license authors from historical records from model.
"""
- out = set()
- for author in [h.license_author for h in set(model.history.all()) if h.license_author]:
- out.add(author)
- return out
+ return set(
+ model.history.exclude(license_author='').values_list('license_author', flat=True).distinct()
+ )
def collect_models_author_history(model_list):