From b8b22d7fd7e3b119a47f07082ac6a93e2cf2ff3d Mon Sep 17 00:00:00 2001 From: "Alexis A." Date: Tue, 5 Nov 2024 11:54:46 +0100 Subject: [PATCH] feat(combined_emprise): add cache to field --- project/models/project_base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/project/models/project_base.py b/project/models/project_base.py index f95d07d54..46bf0c069 100644 --- a/project/models/project_base.py +++ b/project/models/project_base.py @@ -77,12 +77,16 @@ class Status(models.TextChoices): @cached_property def combined_emprise(self) -> MultiPolygon: - """Return a combined MultiPolygon of all emprises.""" + cache_key = f"project/{self.id}/combined_emprise" + if cache.has_key(cache_key): + return cache.get(cache_key) combined = self.emprise_set.aggregate(Union("mpoly")) if "mpoly__union" in combined: - return combined["mpoly__union"] + result = combined["mpoly__union"] else: - return MultiPolygon() + result = MultiPolygon() + cache.set(cache_key, result) + return result def __str__(self): return self.name