Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce repeated calls to django cache while calculating utm_source for listing endpoints #4462

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

zawan-ila
Copy link
Contributor

with override_switch('use_company_name_as_utm_source_value', True):
url = reverse('api:v1:course-list')
self.client.get(url)
mocked_api_access_request.assert_called_once()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we not be checking utm call's count here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_utm_source_for_user function will still be called as many times as it is being currently called. (In this case twice). However, the stuff inside of it (which includes get_api_access_request) will only be called once. And any subsequent invocations will fetch the result from the RequestCache

Copy link
Contributor

@Ali-D-Akbar Ali-D-Akbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionally looks good. Just a bunch of cosmetic suggestions.

@@ -352,3 +353,20 @@ def push_to_studio(self, course_run, create=False, old_course_run_key=None, user
self.create_course_run_in_studio(course_run, user=user)
else:
self.update_course_run_details_in_studio(course_run)


def use_request_cache(cache_name, key_func):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A docstring explaining it's usage would be nice.

Comment on lines +359 to +370
def inner(fn):
@functools.wraps(fn)
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)
cache.set(cache_key, ret_val)
return ret_val
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming and readability can be improved like:
Rename inner to decorator to make it clear that this is a decorator function.
Rename wrapped to wrapper to follow common convention in Python decorator patterns.
Rename ret_val to something like result

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of lines here n there to improve readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[curious] Was the first of these comments generated through some AI tool?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this myself although I got the naming conventions from GPT :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants