-
Notifications
You must be signed in to change notification settings - Fork 171
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
base: master
Are you sure you want to change the base?
Conversation
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this 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): |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
PROD-3968