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

perf: request cache get_schedule_for_user #259

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Change Log
Unreleased
~~~~~~~~~~

[2.5.1] - 2024-08-06
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Reduce schedule queries by using a request cache for get_schedule_for_user.

[2.5.0] - 2024-04-02
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Add support for Python 3.11. Dropped django32 support.
Expand Down
2 changes: 1 addition & 1 deletion edx_when/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Central source of course block dates for the LMS.
"""

__version__ = '2.5.0'
__version__ = '2.5.1'
2 changes: 1 addition & 1 deletion edx_when/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def get_dates_for_course(
user_id = user.id if not user.is_anonymous else ''

if schedule is None and user is not None and user_id != '':
schedule = get_schedule_for_user(user_id, course_id)
schedule = get_schedule_for_user(user_id, course_id, use_cached=use_cached)

# Construct the cache key, incorporating all parameters which would cause a different
# query set to be returned.
Expand Down
36 changes: 29 additions & 7 deletions edx_when/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Utility functions to use across edx-when.
"""
from django.core.exceptions import ObjectDoesNotExist
from edx_django_utils.cache.utils import RequestCache

try:
from openedx.core.djangoapps.schedules.models import Schedule
Expand All @@ -10,13 +11,34 @@
Schedule = None


def get_schedule_for_user(user_id, course_key):
def get_schedule_for_user(user_id, course_key, use_cached=True):
"""
Return the schedule for the user in the course or None if it does not exist or the Schedule model is undefined.
"""
if Schedule:
try:
return Schedule.objects.get(enrollment__user__id=user_id, enrollment__course__id=course_key)
except ObjectDoesNotExist:
pass
return None
# If Schedule is not defined, there's nothing to query, so return None. This
# hackiness is happening because the Schedule model is in edx-platform at
# the moment.
if not Schedule:
return None
Comment on lines +21 to +22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not covered by any tests at this moment, because it never happens in practice–either we're running inside the edx-platform process and the import succeeds, or we're in edx-when tests and it's been mocked with a DummySchedule object.

@ilee2u: I'm inclined to just ignore this omission (no test existed for the similar check it replaced), but I can add a test for this if you think it's warranted.


# This is intentionally a RequestCache and not a TieredCache–that way it's
# just a local memory reference, and we don't have to worry about the
# complications that can come with pickling model objects.
cache = RequestCache('edx-when')
cache_key = f"get_schedule_for_user::{user_id}::{course_key}"
if use_cached:
cache_response = cache.get_cached_response(cache_key)
if cache_response.is_found:
return cache_response.value

try:
schedule = Schedule.objects.get(
enrollment__user__id=user_id,
enrollment__course__id=course_key,
)
except ObjectDoesNotExist:
schedule = None

cache.set(cache_key, schedule)

return schedule
24 changes: 13 additions & 11 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib import auth
from django.test import TestCase
from django.urls import reverse
from edx_django_utils.cache.utils import TieredCache
from edx_django_utils.cache.utils import RequestCache, TieredCache
from opaque_keys.edx.locator import CourseLocator

from edx_when import api, models
Expand Down Expand Up @@ -54,8 +54,11 @@ def setUp(self):
relative_dates_patcher = patch('edx_when.api._are_relative_dates_enabled', return_value=True)
relative_dates_patcher.start()
self.addCleanup(relative_dates_patcher.stop)
self.addCleanup(TieredCache.dangerous_clear_all_tiers)
self.addCleanup(self._clear_caches)

@staticmethod
def _clear_caches():
RequestCache.clear_all_namespaces()
TieredCache.dangerous_clear_all_tiers()

@patch('edx_when.api.Schedule', DummySchedule)
Expand Down Expand Up @@ -274,7 +277,7 @@ def test_set_user_override(self, initial_date, override_date, expected_date):
api.set_dates_for_course(str(block_id.course_key), items)

api.set_date_for_block(block_id.course_key, block_id, 'due', override_date, user=self.user)
TieredCache.dangerous_clear_all_tiers()
self._clear_caches()
retrieved = api.get_dates_for_course(block_id.course_key, user=self.user.id)
assert len(retrieved) == NUM_OVERRIDES
assert retrieved[block_id, 'due'] == expected_date
Expand Down Expand Up @@ -308,7 +311,7 @@ def test_set_date_for_block(self, initial_date, override_date, expected_date):

api.set_dates_for_course(str(block_id.course_key), items)
api.set_date_for_block(block_id.course_key, block_id, 'due', override_date)
TieredCache.dangerous_clear_all_tiers()
self._clear_caches()
retrieved = api.get_dates_for_course(block_id.course_key, user=self.user.id)
assert len(retrieved) == NUM_OVERRIDES
assert retrieved[block_id, 'due'] == expected_date
Expand All @@ -329,13 +332,13 @@ def test_remove_user_override(self, initial_date, override_date, expected_date):
api.set_dates_for_course(str(block_id.course_key), items)

api.set_date_for_block(block_id.course_key, block_id, 'due', override_date, user=self.user)
TieredCache.dangerous_clear_all_tiers()
self._clear_caches()
retrieved = api.get_dates_for_course(block_id.course_key, user=self.user.id)
assert len(retrieved) == NUM_OVERRIDES
assert retrieved[block_id, 'due'] == expected_date

api.set_date_for_block(block_id.course_key, block_id, 'due', None, user=self.user)
TieredCache.dangerous_clear_all_tiers()
self._clear_caches()
retrieved = api.get_dates_for_course(block_id.course_key, user=self.user.id)
assert len(retrieved) == NUM_OVERRIDES
if isinstance(initial_date, timedelta):
Expand Down Expand Up @@ -484,7 +487,7 @@ def test_relative_date_past_cutoff_date(self):
]
assert api.get_dates_for_course(course_key, schedule=self.schedule) == dict(dates)

TieredCache.dangerous_clear_all_tiers()
self._clear_caches()

# Now set schedule start date too close to the end date and verify that we no longer get due dates
self.schedule.created = datetime(2019, 4, 15)
Expand Down Expand Up @@ -526,16 +529,15 @@ def test_get_dates_for_course_query_counts(self, has_schedule, pass_user_object,
course_id=self.course.id, user=user, schedule=schedule
)

# Second time, the request cache eliminates all querying (sometimes)...
# If a schedule is not provided, we will get the schedule to avoid caching outdated dates
Copy link
Contributor Author

Choose a reason for hiding this comment

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

... I'm not sure if it's dangerous that I'm altering this particular behavior, i.e. if there's something that assumes schedules are never cached.

with self.assertNumQueries(0 if schedule else 1):
# Second time, the request cache eliminates all querying...
with self.assertNumQueries(0):
cached_dates = api.get_dates_for_course(
course_id=self.course.id, user=user, schedule=schedule
)
assert dates == cached_dates

# Now wipe all cache tiers...
TieredCache.dangerous_clear_all_tiers()
self._clear_caches()

# No cached values - so will do *all* queries again.
with self.assertNumQueries(query_count):
Expand Down
Loading