Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Move cache handling to serializer

Roland Geider2becb0476051
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 7ccc0a8c0..8b360a4ce 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License
# Django
+from django.conf import settings
+from django.core.cache import cache
from django.db.models import Q
# Third Party
@@ -32,6 +34,7 @@ from wger.exercises.models import (
Muscle,
Variation,
)
+from wger.utils.cache import CacheKeyMapper
class ExerciseBaseSerializer(serializers.ModelSerializer):
@@ -418,3 +421,17 @@ class ExerciseBaseInfoSerializer(serializers.ModelSerializer):
"author_history",
"total_authors_history",
]
+
+ def to_representation(self, instance):
+ """
+ Cache the response
+ """
+ key = CacheKeyMapper.get_exercise_api_key(instance.uuid)
+
+ representation = cache.get(key)
+ if representation:
+ return representation
+
+ representation = super().to_representation(instance)
+ cache.set(key, representation, settings.WGER_SETTINGS['EXERCISE_CACHE_TTL'])
+ return representation
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 03be23e01..a2c4ee26f 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -422,10 +422,6 @@ class ExerciseBaseInfoViewset(viewsets.ReadOnlyModelViewSet):
'license_author',
)
- @method_decorator(cache_page(settings.WGER_SETTINGS['EXERCISE_CACHE_TTL']))
- def dispatch(self, request: HttpRequest, *args, **kwargs):
- return super().dispatch(request, *args, **kwargs)
-
class EquipmentViewSet(viewsets.ReadOnlyModelViewSet):
"""
diff --git a/wger/exercises/management/commands/warmup-api-cache.py b/wger/exercises/management/commands/warmup-api-cache.py
new file mode 100644
index 000000000..d0cbafa40
--- /dev/null
+++ b/wger/exercises/management/commands/warmup-api-cache.py
@@ -0,0 +1,68 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Django
+from django.core.cache import cache
+from django.core.management.base import BaseCommand
+
+from wger.exercises.api.serializers import ExerciseInfoSerializer, ExerciseBaseInfoSerializer
+# wger
+from wger.exercises.models import ExerciseBase
+from wger.utils.cache import CacheKeyMapper
+
+
+class Command(BaseCommand):
+ """
+ Calls the exercise api to get all exercises and caches them in the database.
+ """
+
+ def add_arguments(self, parser):
+ parser.add_argument(
+ '--exercise-base-id',
+ action='store',
+ dest='exercise_base_id',
+ help='The ID of the exercise base, otherwise all exercises will be updated'
+ )
+
+ parser.add_argument(
+ '--force',
+ action='store_true',
+ dest='force',
+ default=False,
+ help='Force the update of the cache'
+ )
+
+ def handle(self, **options):
+ exercise_base_id = options['exercise_base_id']
+ force = options['force']
+
+ if exercise_base_id:
+ exercise = ExerciseBase.objects.get(pk=exercise_base_id)
+ self.handle_cache(exercise, force)
+ return
+
+ for exercise in ExerciseBase.translations.all():
+ self.handle_cache(exercise, force)
+
+ def handle_cache(self, exercise: ExerciseBase, force: bool):
+ if force:
+ self.stdout.write(f"Force updating cache for exercise base {exercise.uuid}")
+ else:
+ self.stdout.write(f"Warming cache for exercise base {exercise.uuid}")
+
+ if force:
+ cache.delete(CacheKeyMapper.get_exercise_api_key(exercise.uuid))
+
+ serializer = ExerciseBaseInfoSerializer(exercise)
+ serializer.data
diff --git a/wger/utils/cache.py b/wger/utils/cache.py
index e16fcbca7..b5c4c2f90 100644
--- a/wger/utils/cache.py
+++ b/wger/utils/cache.py
@@ -58,6 +58,7 @@ class CacheKeyMapper:
WORKOUT_CANONICAL_REPRESENTATION = 'workout-canonical-representation-{0}'
WORKOUT_LOG_LIST = 'workout-log-hash-{0}'
NUTRITION_CACHE_KEY = 'nutrition-cache-log-{0}'
+ EXERCISE_API_KEY = 'base-uuid-{0}'
def get_pk(self, param):
"""
@@ -95,5 +96,12 @@ class CacheKeyMapper:
"""
return self.NUTRITION_CACHE_KEY.format(self.get_pk(params))
+ @classmethod
+ def get_exercise_api_key(cls, base_uuid: str):
+ """
+ get the exercise base cache key used in the API
+ """
+ return cls.EXERCISE_API_KEY.format(base_uuid)
+
cache_mapper = CacheKeyMapper()