explicitly cache counts for collection listings (bug 912484)

This commit is contained in:
Allen Short 2013-09-24 12:50:09 -07:00
Родитель 857a6e35bd
Коммит 5107d662dd
1 изменённых файлов: 10 добавлений и 1 удалений

Просмотреть файл

@ -1,7 +1,9 @@
import functools
import hashlib
import os
from django import http
from django.core.cache import cache
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect
@ -125,7 +127,14 @@ def collection_listing(request, base=None):
return redirect(urlparams(reverse('collections.list'),
sort='followers'), permanent=True)
filter = get_filter(request, base)
collections = paginate(request, filter.qs)
# Counts are hard to cache automatically, and accuracy for this
# one is less important. Remember it for 5 minutes.
countkey = hashlib.md5(str(filter.qs.query) + '_count').hexdigest()
count = cache.get(countkey)
if count is None:
count = filter.qs.count()
cache.set(countkey, count, 300)
collections = paginate(request, filter.qs, count=count)
return render(request, 'bandwagon/impala/collection_listing.html',
dict(collections=collections, src='co-hc-sidebar',
dl_src='co-dp-sidebar', filter=filter, sort=sort,