diff gpp/bio/models.py @ 308:4d086d2210b9

Fixing #141; cache badge ownership to reduce database hits when browsing forum topics.
author Brian Neal <bgneal@gmail.com>
date Sun, 16 Jan 2011 21:06:40 +0000
parents 39664e661c69
children 000c006fee97
line wrap: on
line diff
--- a/gpp/bio/models.py	Sun Jan 16 20:18:26 2011 +0000
+++ b/gpp/bio/models.py	Sun Jan 16 21:06:40 2011 +0000
@@ -114,8 +114,16 @@
     def badge_ownership(self):
         if hasattr(self, '_badges'):
             return self._badges
+
+        cache_key = 'badges-%s' % self.user.username
+        self._badges = cache.get(cache_key)
+        if self._badges is not None:
+            return self._badges
+
         self._badges = BadgeOwnership.objects.filter(profile=self).select_related(
                 "badge")
+
+        cache.set(cache_key, self._badges, 15 * 60)
         return self._badges
 
     def is_stranger(self):