From 59eed7be198e9b161b75a93a5335005c7ac7cafe Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Wed, 3 Jun 2015 15:12:39 -0700 Subject: [PATCH] improve section search performance --- flashcards/models.py | 8 ++++++-- flashy/settings.py | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/flashcards/models.py b/flashcards/models.py index 4551740..595069b 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -468,7 +468,7 @@ class Section(Model): q |= Q(course_num__icontains=term) q |= Q(instructor__icontains=term) final_q &= q - qs = cls.objects.filter(final_q) + qs = cls.objects.filter(final_q).prefetch_related('whitelist') # Have the database cast the course number to an integer so it will sort properly # ECE 35 should rank before ECE 135 even though '135' < '35' lexicographically qs = qs.extra(select={'course_num_int': "CAST(rtrim(course_num, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') AS INTEGER)"}) @@ -480,7 +480,11 @@ class Section(Model): """ :return: whether a whitelist exists for this section """ - return self.whitelist.exists() + data = cache.get("section_%d_is_whitelisted" % self.pk) + if data is None: + data = self.whitelist.exists() + cache.set("section_%d_is_whitelisted" % self.pk, data, 24 * 60 * 60) + return data def is_user_on_whitelist(self, user): """ diff --git a/flashy/settings.py b/flashy/settings.py index 94315ee..bc6c30d 100644 --- a/flashy/settings.py +++ b/flashy/settings.py @@ -121,6 +121,7 @@ EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' if DEBUG: INSTALLED_APPS.append('debug_toolbar') + # DEBUG_TOOLBAR_PANELS = ['debug_toolbar.panels.profiling.ProfilingPanel'] if IN_PRODUCTION: INSTALLED_APPS.append('django_ses') -- 1.9.1