From 16aa01a8820d31fdc413a133891329713494c9b4 Mon Sep 17 00:00:00 2001 From: ffont Date: Tue, 31 Oct 2023 13:13:12 +0100 Subject: [PATCH] Use template cache in latest packs section --- accounts/models.py | 5 +++++ accounts/views.py | 12 +++--------- freesound/settings.py | 1 - .../accounts/account_latest_packs_section.html | 7 ++++++- utils/cache.py | 3 ++- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/accounts/models.py b/accounts/models.py index c63256ba4..b048e05b1 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -225,6 +225,11 @@ def get_user_sounds_in_search_url(self): def get_user_packs_in_search_url(self): return f'{reverse("sounds-search")}?f=username:"{ self.user.username }"&s=Date+added+(newest+first)&g=1&only_p=1' + + def get_latest_packs_for_profile_page(self): + latest_pack_ids = Pack.objects.select_related().filter(user=self.user, num_sounds__gt=0).exclude(is_deleted=True) \ + .order_by("-last_updated").values_list('id', flat=True)[0:15] + return Pack.objects.ordered_ids(pack_ids=latest_pack_ids) @staticmethod def locations_static(user_id, has_avatar): diff --git a/accounts/views.py b/accounts/views.py index 41950d399..ce8fbc705 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1485,18 +1485,12 @@ def account_stats_section(request, username): def account_latest_packs_section(request, username): if not request.GET.get('ajax'): raise Http404 # Only accessible via ajax + user = request.parameter_user - latest_packs_from_cache = cache.get(settings.USER_LATEST_PACKS_CACHE_KEY.format(user.id), None) - if latest_packs_from_cache is None: - latest_pack_ids = Pack.objects.select_related().filter(user=user, num_sounds__gt=0).exclude(is_deleted=True) \ - .order_by("-last_updated").values_list('id', flat=True)[0:15] - latest_packs = Pack.objects.ordered_ids(pack_ids=latest_pack_ids) - cache.set(settings.USER_LATEST_PACKS_CACHE_KEY.format(user.id), pickle.dumps(latest_packs), 60 * 60 * 24) - else: - latest_packs = pickle.loads(latest_packs_from_cache) tvars = { 'user': user, - 'latest_packs': latest_packs, + # Note we don't pass latest packs data because it is requested from the template + # if there is no cache available } return render(request, 'accounts/account_latest_packs_section.html', tvars) diff --git a/freesound/settings.py b/freesound/settings.py index 49d381a05..a59a4c889 100644 --- a/freesound/settings.py +++ b/freesound/settings.py @@ -353,7 +353,6 @@ # User profile page cache key templates USER_STATS_CACHE_KEY = 'user_stats_{}' -USER_LATEST_PACKS_CACHE_KEY = 'user_latest_packs_{}' # User flagging notification thresholds USERFLAG_THRESHOLD_FOR_NOTIFICATION = 3 diff --git a/templates_bw/accounts/account_latest_packs_section.html b/templates_bw/accounts/account_latest_packs_section.html index f6385d50e..c384c97ee 100644 --- a/templates_bw/accounts/account_latest_packs_section.html +++ b/templates_bw/accounts/account_latest_packs_section.html @@ -1,4 +1,7 @@ {% load display_pack %} +{% load cache %} +{% cache 43200 bw_user_profile_latest_packs_section is_authenticated %} +{% with user.profile.get_latest_packs_for_profile_page as latest_packs %} {% if latest_packs %}
{% for pack in latest_packs %} @@ -15,4 +18,6 @@
No packs... 😟
Looks like {{ user.username }} has not uploaded any packs yet
-{% endif %} \ No newline at end of file +{% endif %} +{% endwith %} +{% endcache %} \ No newline at end of file diff --git a/utils/cache.py b/utils/cache.py index 082981ec0..597e5e5b8 100644 --- a/utils/cache.py +++ b/utils/cache.py @@ -40,8 +40,9 @@ def invalidate_user_template_caches(user_id): invalidate_template_cache('bw_user_profile_followers_count', user_id) invalidate_template_cache('bw_user_profile_following_count', user_id) invalidate_template_cache('bw_user_profile_following_tags_count', user_id) + invalidate_template_cache('bw_user_profile_latest_packs_section', True) + invalidate_template_cache('bw_user_profile_latest_packs_section', False) cache.delete(settings.USER_STATS_CACHE_KEY.format(user_id)) - cache.delete(settings.USER_LATEST_PACKS_CACHE_KEY.format(user_id)) def invalidate_all_moderators_header_cache():