EDUCATOR-2618 request cache some more stuff

This commit is contained in:
Sanford Student
2018-04-25 13:11:49 -04:00
parent 321a1acb04
commit 2f0e3d35a0
3 changed files with 17 additions and 7 deletions

View File

@@ -44,6 +44,7 @@ from lms.lib.comment_client.utils import CommentClientPaginatedResult
from openedx.core.djangoapps.course_groups.models import CourseUserGroup
from openedx.core.djangoapps.course_groups.tests.helpers import config_course_cohorts
from openedx.core.djangoapps.course_groups.tests.test_views import CohortViewsTestCase
from openedx.core.djangoapps.request_cache.middleware import RequestCache
from openedx.core.djangoapps.util.testing import ContentGroupTestCase
from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseTestConsentRequired
@@ -404,15 +405,15 @@ class SingleThreadQueryCountTestCase(ForumsEnableMixin, ModuleStoreTestCase):
# course is outside the context manager that is verifying the number of queries,
# and with split mongo, that method ends up querying disabled_xblocks (which is then
# cached and hence not queried as part of call_single_thread).
(ModuleStoreEnum.Type.mongo, False, 1, 6, 4, 16, 4),
(ModuleStoreEnum.Type.mongo, False, 50, 6, 4, 16, 4),
(ModuleStoreEnum.Type.mongo, False, 1, 5, 2, 16, 4),
(ModuleStoreEnum.Type.mongo, False, 50, 5, 2, 16, 4),
# split mongo: 3 queries, regardless of thread response size.
(ModuleStoreEnum.Type.split, False, 1, 3, 3, 16, 4),
(ModuleStoreEnum.Type.split, False, 50, 3, 3, 16, 4),
# Enabling Enterprise integration should have no effect on the number of mongo queries made.
(ModuleStoreEnum.Type.mongo, True, 1, 6, 4, 16, 4),
(ModuleStoreEnum.Type.mongo, True, 50, 6, 4, 16, 4),
(ModuleStoreEnum.Type.mongo, True, 1, 5, 2, 16, 4),
(ModuleStoreEnum.Type.mongo, True, 50, 5, 2, 16, 4),
# split mongo: 3 queries, regardless of thread response size.
(ModuleStoreEnum.Type.split, True, 1, 3, 3, 16, 4),
(ModuleStoreEnum.Type.split, True, 50, 3, 3, 16, 4),
@@ -1854,6 +1855,7 @@ class CourseDiscussionsHandlerTestCase(DividedDiscussionsTestCase):
expected_response = self.get_expected_response()
self.assertEqual(response, expected_response)
RequestCache.clear_request_cache()
now = datetime.now()
# inline discussion
ItemFactory.create(

View File

@@ -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
@@ -137,7 +138,6 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
"""
def setUp(self):
super(CoursewareContextTestCase, self).setUp()
self.course = CourseFactory.create(org="TestX", number="101", display_name="Test Course")
self.discussion1 = ItemFactory.create(
parent_location=self.course.location,
@@ -224,6 +224,8 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
# Assert that there is only one discussion xblock in the course at the moment.
self.assertEqual(len(utils.get_accessible_discussion_xblocks(course, self.user)), 1)
# The above call is request cached, so we need to clear it for this test.
RequestCache.clear_request_cache()
# Add an orphan discussion xblock to that course
orphan = course.id.make_usage_key('discussion', 'orphan_discussion')
self.store.create_item(self.user.id, orphan.course_key, orphan.block_type, block_id=orphan.block_id)

View File

@@ -131,6 +131,7 @@ def get_accessible_discussion_xblocks(course, user, include_all=False): # pylin
return get_accessible_discussion_xblocks_by_course_id(course.id, user, include_all=include_all)
@request_cached
def get_accessible_discussion_xblocks_by_course_id(course_id, user, include_all=False): # pylint: disable=invalid-name
"""
Return a list of all valid discussion xblocks in this course that
@@ -198,7 +199,7 @@ def get_cached_discussion_id_map_by_course_id(course_id, discussion_ids, user):
key = get_cached_discussion_key(course_id, discussion_id)
if not key:
continue
xblock = modulestore().get_item(key)
xblock = _get_item_from_modulestore(key)
if not (has_required_keys(xblock) and has_access(user, 'load', xblock, course_id)):
continue
entries.append(get_discussion_id_map_entry(xblock))
@@ -224,6 +225,11 @@ def get_discussion_id_map_by_course_id(course_id, user): # pylint: disable=inva
return dict(map(get_discussion_id_map_entry, xblocks))
@request_cached
def _get_item_from_modulestore(key):
return modulestore().get_item(key)
def _filter_unstarted_categories(category_map, course):
"""
Returns a subset of categories from the provided map which have not yet met the start date
@@ -434,7 +440,7 @@ def discussion_category_id_access(course, user, discussion_id, xblock=None):
key = get_cached_discussion_key(course.id, discussion_id)
if not key:
return False
xblock = modulestore().get_item(key)
xblock = _get_item_from_modulestore(key)
return has_required_keys(xblock) and has_access(user, 'load', xblock, course.id)
except DiscussionIdMapIsNotCached:
return discussion_id in get_discussion_categories_ids(course, user)