Skip to content

Commit

Permalink
style: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila committed Oct 10, 2024
1 parent 51f261c commit 8138d20
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion course_discovery/apps/api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)

def get_utm_source_request_cache_key(partner, user):
return get_cache_key(partner=partner.id, user=user.id)
return get_cache_key(partner=partner.id, user=user.id)
1 change: 0 additions & 1 deletion course_discovery/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_countries.serializer_fields import CountryField
from edx_django_utils.cache import RequestCache, get_cache_key
from localflavor.us.us_states import CONTIGUOUS_STATES
from opaque_keys.edx.locator import CourseLocator
from rest_flex_fields.serializers import FlexFieldsSerializerMixin
Expand Down
9 changes: 4 additions & 5 deletions course_discovery/apps/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.files.base import ContentFile
from django.db.models.fields.related import ManyToManyField
from django.utils.translation import gettext as _
from edx_django_utils.cache import RequestCache, get_cache_key
from edx_django_utils.cache import RequestCache
from opaque_keys.edx.keys import CourseKey
from requests.exceptions import HTTPError
from sortedm2m.fields import SortedManyToManyField
Expand Down Expand Up @@ -357,16 +357,15 @@ def push_to_studio(self, course_run, create=False, old_course_run_key=None, user
def use_request_cache(cache_name, key_func):
def inner(fn):
@functools.wraps(fn)
def foo(*args, **kwargs):
def wrapped(*args, **kwargs):
cache = RequestCache(cache_name)
cache_key = key_func(*args, **kwargs)
cached_response = cache.get_cached_response(cache_key)
if cached_response.is_found:
return cached_response.value

ret_val = fn(*args, **kwargs)

ret_val = fn(*args, **kwargs)
cache.set(cache_key, ret_val)
return ret_val
return foo
return wrapped
return inner
5 changes: 5 additions & 0 deletions course_discovery/apps/api/v1/tests/test_views/test_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ def test_list(self):
)

def test_no_repeated_cache_calls_for_utm_calculation(self):
"""
Test that utm source calculation is done only once per request, and not per
serialized object on a listing endpoint. Since each utm source calculation
requires two cache calls, this reduces the number of calls to the django cache.
"""
CourseFactory(
partner=self.partner, title='Fake Test 1', key='edX+Fake102', type=self.audit_type
)
Expand Down

0 comments on commit 8138d20

Please sign in to comment.