Skip to content

Commit

Permalink
Cache user latest packs query
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Oct 31, 2023
1 parent fe54200 commit 44a4ce1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import json
import logging
import os
import pickle
import tempfile
import time
import uuid
Expand Down Expand Up @@ -1485,9 +1486,14 @@ def account_latest_packs_section(request, username):
if not request.GET.get('ajax'):
raise Http404 # Only accessible via ajax
user = request.parameter_user
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)
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,
Expand Down
3 changes: 2 additions & 1 deletion freesound/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@
# Whether or not user bookmarks and bookmark categories should be public (visible to all other users) in BW
BW_BOOKMARK_PAGES_PUBLIC = False

# User stats cache key template
# 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
Expand Down
1 change: 1 addition & 0 deletions utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def invalidate_user_template_caches(user_id):
invalidate_template_cache('bw_user_profile_following_count', user_id)
invalidate_template_cache('bw_user_profile_following_tags_count', user_id)
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():
Expand Down

0 comments on commit 44a4ce1

Please sign in to comment.