diff gpp/forums/models.py @ 387:b15726767ab8

Fixing #191; terrible performance on the combined forums RSS feed query. Use an .extra() clause to force the WHERE on a query to use the primary key.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Mar 2011 01:52:41 +0000
parents 9fcd366f22dc
children 9af6bd45c1f8
line wrap: on
line diff
--- a/gpp/forums/models.py	Thu Mar 17 01:20:23 2011 +0000
+++ b/gpp/forums/models.py	Sat Mar 19 01:52:41 2011 +0000
@@ -6,6 +6,7 @@
 from django.db import models
 from django.db.models import Q
 from django.contrib.auth.models import User, Group
+from django.core.cache import cache
 
 from core.markup import site_markup
 from oembed.models import Oembed
@@ -71,6 +72,18 @@
         """Returns a queryset containing the public forums."""
         return self.filter(category__groups__isnull=True)
 
+    def public_forum_ids(self):
+        """
+        Returns a list of ids for the public forums; the list is cached for
+        performance.
+        """
+        public_forums = cache.get('public_forum_ids')
+        if public_forums is None:
+            public_forums = list(self.filter(
+                category__groups__isnull=True).values_list('id', flat=True))
+            cache.set('public_forum_ids', public_forums, 3600)
+        return public_forums
+
     def _for_user(self, user):
         """Common code for the xxx_for_user() methods."""
         if user.is_superuser: