-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(implement-cache) Enable Cache nutritional values for plan
This commit cache the nutritional_info dictionary in NutritionPlans and add signals that delete the cache Currently, the nutrition plans is not cached so If a user has many plans, the overview renders slowly as too many DB-queries are fired just to calculate the total calories. Some caching of the values will speed up rendering of the overview [Delivers #157731610]
- Loading branch information
1 parent
ed956c8
commit 2397166
Showing
17 changed files
with
207 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,4 +91,3 @@ class MuscleSerializer(serializers.ModelSerializer): | |
''' | ||
class Meta: | ||
model = Muscle | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# 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 | ||
|
||
from django.apps import AppConfig | ||
|
||
|
||
class NutritionPlanConfig(AppConfig): | ||
name = 'wger.nutrition' | ||
verbose_name = "Nutrition" | ||
|
||
def ready(self): | ||
import wger.nutrition.signals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# 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 | ||
|
||
|
||
from django.db.models.signals import post_save, post_delete | ||
from django.dispatch import receiver | ||
|
||
from wger.nutrition.models import NutritionPlan, Meal, MealItem | ||
from wger.utils.cache import reset_nutritional_values_canonical_form | ||
|
||
|
||
@receiver(post_save, sender=NutritionPlan) | ||
@receiver(post_save, sender=Meal) | ||
@receiver(post_save, sender=MealItem) | ||
@receiver(post_delete, sender=NutritionPlan) | ||
@receiver(post_delete, sender=Meal) | ||
@receiver(post_delete, sender=MealItem) | ||
def delete_cache(sender, **kwargs): | ||
""" Function for intercepting signals """ | ||
|
||
sender_instance = kwargs['instance'] | ||
if sender == NutritionPlan: | ||
pk = sender_instance.pk | ||
elif sender == Meal: | ||
pk = sender_instance.plan.pk | ||
elif sender == MealItem: | ||
pk = sender_instance.meal.plan.pk | ||
|
||
reset_nutritional_values_canonical_form(pk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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 | ||
|
||
|
||
import datetime | ||
from django.core.cache import cache | ||
from wger.utils.cache import cache_mapper | ||
from wger.core.tests.base_testcase import WorkoutManagerTestCase | ||
from wger.nutrition.models import NutritionPlan | ||
|
||
|
||
class NutritionInfoCacheTestCase(WorkoutManagerTestCase): | ||
''' | ||
Test case for the nutritional values caching | ||
''' | ||
|
||
def test_meal_nutritional_values_cache(self): | ||
''' | ||
Tests that the nutrition cache of the canonical form is | ||
correctly generated | ||
''' | ||
self.assertFalse(cache.get(cache_mapper.get_nutritional_values_canonical(1))) | ||
|
||
plan = NutritionPlan.objects.get(pk=1) | ||
plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_values_canonical(1))) | ||
|
||
def test_nutritional_values_cache_save(self): | ||
''' | ||
Tests nutritional values cache when saving | ||
''' | ||
plan = NutritionPlan.objects.get(pk=1) | ||
plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_values_canonical(1))) | ||
|
||
plan.save() | ||
self.assertFalse(cache.get(cache_mapper.get_nutritional_values_canonical(1))) | ||
|
||
def test_nutritional_values_cache_delete(self): | ||
''' | ||
Tests the nutritional values cache when deleting | ||
''' | ||
plan = NutritionPlan.objects.get(pk=1) | ||
plan.get_nutritional_values() | ||
self.assertTrue(cache.get(cache_mapper.get_nutritional_values_canonical(1))) | ||
|
||
plan.delete() | ||
self.assertFalse(cache.get(cache_mapper.get_nutritional_values_canonical(1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,4 +412,3 @@ | |
|
||
if 'DATABASE_URL' in os.environ: | ||
DATABASES = {'default': dj_database_url.config()} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.