changeset 314:03b70aa5c872

The home page was taking 27 seconds to load; 99% of the time was being spent in the new_posts template tag query. Reworked it a bit to be much more sane.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 Jan 2011 20:34:36 +0000
parents 377dd359636f
children 36373d995611
files gpp/forums/templatetags/forum_tags.py
diffstat 1 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/templatetags/forum_tags.py	Thu Jan 27 03:00:16 2011 +0000
+++ b/gpp/forums/templatetags/forum_tags.py	Sat Jan 29 20:34:36 2011 +0000
@@ -12,6 +12,7 @@
 from forums.models import Forum
 from forums.models import Topic
 from forums.models import Post
+from forums.models import Category
 from core.models import Statistic
 
 
@@ -119,10 +120,26 @@
     This tag displays the topics that have the newest posts.
     Only the "public" forums are displayed.
     """
-    topics = Topic.objects.filter(
-            forum__category__groups__isnull=True).select_related(
+    public_forums = cache.get('public_forum_ids')
+    if public_forums is None:
+        public_forums = list(Forum.objects.filter(
+            category__groups__isnull=True).values_list('id', flat=True))
+        cache.set('public_forum_ids', public_forums, 10 * 60)
+
+    topics = Topic.objects.filter(forum__in=public_forums).select_related(
                     'user', 'last_post').order_by('-update_date')[:10]
 
+    # 1:
+    #public_cats = list(Category.objects.filter(groups__isnull=True).values_list('id', flat=True))
+    #topics = Topic.objects.filter(
+    #        forum__category__in=public_cats).select_related(
+    #                'user', 'last_post').order_by('-update_date')[:10]
+
+    # 0:
+    #topics = Topic.objects.filter(
+    #        forum__category__groups__isnull=True).select_related(
+    #                'user', 'last_post').order_by('-update_date')[:10]
+
     return {
         'topics': topics,
         'user': context['user'],