Commit 7985cc96 by Michael Frey Committed by GitHub

Merge pull request #857 from edx/mjfrey/cache-catalog-contains

Mjfrey/cache catalog contains
parents bccd44b3 e70276fd
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
import hashlib
from django.conf import settings
from django.core.cache import cache
from django.db import models from django.db import models
from oscar.apps.offer.abstract_models import AbstractRange from oscar.apps.offer.abstract_models import AbstractRange
from threadlocals.threadlocals import get_current_request from threadlocals.threadlocals import get_current_request
...@@ -15,14 +19,20 @@ class Range(AbstractRange): ...@@ -15,14 +19,20 @@ class Range(AbstractRange):
""" """
Retrieve the results from running the query contained in catalog_query field. Retrieve the results from running the query contained in catalog_query field.
""" """
cache_key = 'catalog_query_contains [{}] [{}]'.format(self.catalog_query, product.course_id)
cache_hash = hashlib.md5(cache_key).hexdigest()
response = cache.get(cache_hash)
if not response: # pragma: no cover
request = get_current_request() request = get_current_request()
try: try:
response = request.site.siteconfiguration.course_catalog_api_client.course_runs.contains.get( response = request.site.siteconfiguration.course_catalog_api_client.course_runs.contains.get(
query=self.catalog_query, query=self.catalog_query,
course_run_ids=product.course_id course_run_ids=product.course_id
) )
cache.set(cache_hash, response, settings.COURSES_API_CACHE_TIMEOUT)
except: # pylint: disable=bare-except except: # pylint: disable=bare-except
raise Exception('Could not contact Course Catalog Service.') raise Exception('Could not contact Course Catalog Service.')
return response return response
def contains_product(self, product): def contains_product(self, product):
......
import hashlib
import httpretty import httpretty
import mock import mock
from django.core.cache import cache
from django.test import RequestFactory from django.test import RequestFactory
from oscar.core.loading import get_model from oscar.core.loading import get_model
from oscar.test import factories from oscar.test import factories
...@@ -73,10 +76,18 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te ...@@ -73,10 +76,18 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
self.mock_dynamic_catalog_contains_api(query='key:*', course_run_ids=[course.id]) self.mock_dynamic_catalog_contains_api(query='key:*', course_run_ids=[course.id])
request = RequestFactory() request = RequestFactory()
request.site = self.site request.site = self.site
self.range.catalog_query = 'key:*'
cache_key = 'catalog_query_contains [{}] [{}]'.format('key:*', seat.course_id)
cache_hash = hashlib.md5(cache_key).hexdigest()
cached_response = cache.get(cache_hash)
self.assertIsNone(cached_response)
with mock.patch('ecommerce.core.url_utils.get_current_request', mock.Mock(return_value=request)): with mock.patch('ecommerce.core.url_utils.get_current_request', mock.Mock(return_value=request)):
response = self.range.run_catalog_query(seat) response = self.range.run_catalog_query(seat)
self.assertTrue(response['course_runs'][course.id]) self.assertTrue(response['course_runs'][course.id])
cached_response = cache.get(cache_hash)
self.assertEqual(response, cached_response)
@httpretty.activate @httpretty.activate
@mock_course_catalog_api_client @mock_course_catalog_api_client
...@@ -86,6 +97,7 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te ...@@ -86,6 +97,7 @@ class RangeTests(CouponMixin, CourseCatalogTestMixin, CourseCatalogMockMixin, Te
""" """
course, seat = self.create_course_and_seat() course, seat = self.create_course_and_seat()
self.mock_dynamic_catalog_contains_api(query='key:*', course_run_ids=[course.id]) self.mock_dynamic_catalog_contains_api(query='key:*', course_run_ids=[course.id])
false_response = self.range.contains_product(seat) false_response = self.range.contains_product(seat)
self.assertFalse(false_response) self.assertFalse(false_response)
......
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