From 1624720f48a511304833bd59dc9e8d807bd43e9c Mon Sep 17 00:00:00 2001 From: Will Daly Date: Fri, 20 Feb 2015 14:17:14 -0500 Subject: [PATCH] Stop hitting the database when no courses are blocked. --- common/djangoapps/embargo/models.py | 4 ++-- common/djangoapps/embargo/tests/test_api.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/djangoapps/embargo/models.py b/common/djangoapps/embargo/models.py index c254c45194..447c023fd6 100644 --- a/common/djangoapps/embargo/models.py +++ b/common/djangoapps/embargo/models.py @@ -156,7 +156,7 @@ class RestrictedCourse(models.Model): Cache all restricted courses and returns the list of course_keys that are restricted """ restricted_courses = cache.get(cls.COURSE_LIST_CACHE_KEY) - if not restricted_courses: + if restricted_courses is None: restricted_courses = list(RestrictedCourse.objects.values_list('course_key', flat=True)) cache.set(cls.COURSE_LIST_CACHE_KEY, restricted_courses) return restricted_courses @@ -413,7 +413,7 @@ class CountryAccessRule(models.Model): """ cache_key = cls.CACHE_KEY.format(course_key=course_id) allowed_countries = cache.get(cache_key) - if not allowed_countries: + if allowed_countries is None: allowed_countries = cls._get_country_access_list(course_id) cache.set(cache_key, allowed_countries) diff --git a/common/djangoapps/embargo/tests/test_api.py b/common/djangoapps/embargo/tests/test_api.py index f0df5d3f10..299e59defe 100644 --- a/common/djangoapps/embargo/tests/test_api.py +++ b/common/djangoapps/embargo/tests/test_api.py @@ -166,6 +166,16 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase): with self.assertNumQueries(0): embargo_api.check_course_access(self.course.id, user=self.user, ip_address='0.0.0.0') + def test_caching_no_restricted_courses(self): + RestrictedCourse.objects.all().delete() + cache.clear() + + with self.assertNumQueries(1): + embargo_api.check_course_access(self.course.id, user=self.user, ip_address='0.0.0.0') + + with self.assertNumQueries(0): + embargo_api.check_course_access(self.course.id, user=self.user, ip_address='0.0.0.0') + @ddt.ddt @override_settings(MODULESTORE=MODULESTORE_CONFIG)