Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Refactor cache keys to include user ID for the routine API endpoints

Roland Geidere964328784e2
diff --git a/wger/manager/api/views.py b/wger/manager/api/views.py
index 94aeee579..701d68226 100644
--- a/wger/manager/api/views.py
+++ b/wger/manager/api/views.py
@@ -128,10 +128,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Return the day sequence of the routine
"""
- # profiler = cProfile.Profile()
- # profiler.enable()
-
- cache_key = CacheKeyMapper.routine_api_date_sequence_display_key(pk)
+ cache_key = CacheKeyMapper.routine_api_date_sequence_display_key(pk, request.user.id)
cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
@@ -142,9 +139,6 @@ class RoutineViewSet(viewsets.ModelViewSet):
).data
cache.set(cache_key, out, settings.WGER_SETTINGS['ROUTINE_CACHE_TTL'])
- # profiler.disable()
- # profiler.dump_stats("profile_results.prof")
-
return Response(out)
@action(detail=True, url_path='date-sequence-gym')
@@ -152,7 +146,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Return the day sequence of the routine
"""
- cache_key = CacheKeyMapper.routine_api_date_sequence_gym_key(pk)
+ cache_key = CacheKeyMapper.routine_api_date_sequence_gym_key(pk, request.user.id)
cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
@@ -167,7 +161,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Return the full object structure of the routine.
"""
- cache_key = CacheKeyMapper.routine_api_structure_key(pk)
+ cache_key = CacheKeyMapper.routine_api_structure_key(pk, request.user.id)
cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
@@ -181,7 +175,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Returns the logs for the routine
"""
- cache_key = CacheKeyMapper.routine_api_logs(pk)
+ cache_key = CacheKeyMapper.routine_api_logs(pk, request.user.id)
cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
@@ -195,7 +189,7 @@ class RoutineViewSet(viewsets.ModelViewSet):
"""
Returns the logs for the routine
"""
- cache_key = CacheKeyMapper.routine_api_stats(pk)
+ cache_key = CacheKeyMapper.routine_api_stats(pk, request.user.id)
cached_data = cache.get(cache_key)
if cached_data is not None:
return Response(cached_data)
diff --git a/wger/manager/helpers.py b/wger/manager/helpers.py
index 43e1fef36..999ffea76 100644
--- a/wger/manager/helpers.py
+++ b/wger/manager/helpers.py
@@ -186,12 +186,13 @@ def reset_routine_cache(instance: Routine, structure: bool = True):
"""Resets all caches related to a routine"""
cache.delete(CacheKeyMapper.routine_date_sequence_key(instance.id))
- cache.delete(CacheKeyMapper.routine_api_date_sequence_display_key(instance.id))
- cache.delete(CacheKeyMapper.routine_api_date_sequence_gym_key(instance.id))
- cache.delete(CacheKeyMapper.routine_api_stats(instance.id))
+ cache.delete(CacheKeyMapper.routine_api_date_sequence_display_key(instance.id, instance.user_id))
+ cache.delete(CacheKeyMapper.routine_api_date_sequence_gym_key(instance.id, instance.user_id))
+ cache.delete(CacheKeyMapper.routine_api_logs(instance.id, instance.user_id))
+ cache.delete(CacheKeyMapper.routine_api_stats(instance.id, instance.user_id))
if structure:
- cache.delete(CacheKeyMapper.routine_api_structure_key(instance.id))
+ cache.delete(CacheKeyMapper.routine_api_structure_key(instance.id, instance.user_id))
if instance.pk:
for day in instance.days.all():
diff --git a/wger/manager/signals.py b/wger/manager/signals.py
index a98b89efe..068dd7593 100644
--- a/wger/manager/signals.py
+++ b/wger/manager/signals.py
@@ -77,14 +77,14 @@ def handle_config_change(sender, instance: AbstractChangeConfig, **kwargs):
def handle_workout_log_change(sender, instance: WorkoutLog, **kwargs):
update_activity_cache(sender, instance, **kwargs)
if instance.routine:
- cache.delete(CacheKeyMapper.routine_api_logs(instance.routine.id))
+ cache.delete(CacheKeyMapper.routine_api_logs(instance.routine.id, instance.user_id))
reset_routine_cache(instance.routine, structure=False)
def handle_workout_session_change(sender, instance: WorkoutSession, **kwargs):
update_activity_cache(sender, instance, **kwargs)
if instance.routine:
- cache.delete(CacheKeyMapper.routine_api_logs(instance.routine.id))
+ cache.delete(CacheKeyMapper.routine_api_logs(instance.routine.id, instance.user_id))
reset_routine_cache(instance.routine, structure=False)
diff --git a/wger/utils/cache.py b/wger/utils/cache.py
index 635953d62..e214c602e 100644
--- a/wger/utils/cache.py
+++ b/wger/utils/cache.py
@@ -82,28 +82,28 @@ class CacheKeyMapper:
return f'base-uuid-{base_uuid}'
@classmethod
- def routine_date_sequence_key(cls, id: int):
- return f'routine-date-sequence-{id}'
+ def routine_date_sequence_key(cls, pk: int):
+ return f'routine-date-sequence-{pk}'
@classmethod
- def routine_api_date_sequence_display_key(cls, pk: int):
- return f'routine-api-date-sequence-display-{pk}'
+ def routine_api_date_sequence_display_key(cls, pk: int, user_id: int):
+ return f'routine-api-date-sequence-display-{user_id}-{pk}'
@classmethod
- def routine_api_date_sequence_gym_key(cls, pk: int):
- return f'routine-api-date-sequence-gym-{pk}'
+ def routine_api_date_sequence_gym_key(cls, pk: int, user_id: int):
+ return f'routine-api-date-sequence-gym-{user_id}-{pk}'
@classmethod
- def routine_api_stats(cls, pk: int):
- return f'routine-api-stats-{pk}'
+ def routine_api_stats(cls, pk: int, user_id: int):
+ return f'routine-api-stats-{user_id}-{pk}'
@classmethod
- def routine_api_logs(cls, pk: int):
- return f'routine-api-logs-{pk}'
+ def routine_api_logs(cls, pk: int, user_id: int):
+ return f'routine-api-logs-{user_id}-{pk}'
@classmethod
- def routine_api_structure_key(cls, pk: int):
- return f'routine-api-structure-{pk}'
+ def routine_api_structure_key(cls, pk: int, user_id: int = None):
+ return f'routine-api-structure-{user_id}-{pk}'
@classmethod
def slot_entry_configs_key(cls, pk: int):