Commit 1624720f by Will Daly

Stop hitting the database when no courses are blocked.

parent 0e3f40b9
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment