From 30476a338f645f1fae69807bc441f99723593c6d Mon Sep 17 00:00:00 2001 From: Eric Fischer <efischer@edx.org> Date: Wed, 25 Apr 2018 13:00:24 -0400 Subject: [PATCH] Stop hitting mongo so much, cache this result --- .../django_comment_client/tests/test_utils.py | 2 ++ lms/djangoapps/django_comment_client/utils.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/django_comment_client/tests/test_utils.py b/lms/djangoapps/django_comment_client/tests/test_utils.py index 3a0b68b4fbb..65a0b613be6 100644 --- a/lms/djangoapps/django_comment_client/tests/test_utils.py +++ b/lms/djangoapps/django_comment_client/tests/test_utils.py @@ -38,6 +38,7 @@ from openedx.core.djangoapps.content.course_structures.models import CourseStruc from openedx.core.djangoapps.course_groups import cohorts from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory, config_course_cohorts +from openedx.core.djangoapps.request_cache.middleware import RequestCache from openedx.core.djangoapps.util.testing import ContentGroupTestCase from student.roles import CourseStaffRole from student.tests.factories import AdminFactory, CourseEnrollmentFactory, UserFactory @@ -1785,6 +1786,7 @@ class GroupModeratorPermissionsTestCase(ModuleStoreTestCase): 'can_vote': True, 'can_report': True }) + RequestCache.clear_request_cache() set_discussion_division_settings(self.course.id, division_scheme=CourseDiscussionSettings.ENROLLMENT_TRACK) content = {'user_id': self.verified_user.id, 'type': 'thread', 'username': self.verified_user.username} diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index cd197ac2324..edfba0b0d14 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -592,15 +592,14 @@ def get_user_group_ids(course_id, content, user=None): content_user_group_id = None user_group_id = None if course_id is not None: - course_discussion_settings = get_course_discussion_settings(course_id) if content.get('username'): try: content_user = get_user_by_username_or_email(content.get('username')) - content_user_group_id = get_group_id_for_user(content_user, course_discussion_settings) + content_user_group_id = get_group_id_for_user_from_cache(content_user, course_id) except User.DoesNotExist: content_user_group_id = None - user_group_id = get_group_id_for_user(user, course_discussion_settings) if user else None + user_group_id = get_group_id_for_user_from_cache(user, course_id) if user else None return user_group_id, content_user_group_id @@ -819,7 +818,7 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None): _verify_group_exists(group_id, course_discussion_settings) else: # regular users always query with their own id. - group_id = get_group_id_for_user(request.user, course_discussion_settings) + group_id = get_group_id_for_user_from_cache(request.user, course_key) return group_id else: # Never pass a group_id to the comments service for a non-divided @@ -827,6 +826,15 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None): return None +@request_cached +def get_group_id_for_user_from_cache(user, course_id): + """ + Caches the results of get_group_id_for_user, but serializes the course_id + instead of the course_discussions_settings object as cache keys. + """ + return get_group_id_for_user(user, get_course_discussion_settings(course_id)) + + def get_group_id_for_user(user, course_discussion_settings): """ Given a user, return the group_id for that user according to the course_discussion_settings. -- GitLab