Skip to content
Snippets Groups Projects
Unverified Commit 4f1af19e authored by emma-green's avatar emma-green Committed by GitHub
Browse files

Merge pull request #19882 from edx/emma-green/REVEM-176/D/Fix-course-program-cache

fix course program cache
parents 4b8d6a92 f36673af
No related merge requests found
""""Management command to add program information to the cache."""
from collections import defaultdict
import logging
import sys
......@@ -211,15 +213,12 @@ class Command(BaseCommand):
in the cache not just the course runs in a program. Therefore, a cache miss would be different from a
course not in a program.
"""
course_runs = {}
course_runs = defaultdict(list)
failure = False
for program_uuid, program in programs.items():
for program in programs.values():
for course in program['courses']:
for course_run in course['course_runs']:
course_run_key = course_run['key']
if course_run_key in course_runs:
course_runs[course_run_key] += program_uuid
else:
course_runs[course_run_key] = [program_uuid]
course_run_cache_key = COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_run_id=course_run['key'])
course_runs[course_run_cache_key].append(program['uuid'])
return course_runs, failure
......@@ -16,6 +16,7 @@ from course_modes.tests.factories import CourseModeFactory
from entitlements.tests.factories import CourseEntitlementFactory
from openedx.core.constants import COURSE_UNPUBLISHED
from openedx.core.djangoapps.catalog.cache import (
COURSE_PROGRAMS_CACHE_KEY_TPL,
PATHWAY_CACHE_KEY_TPL,
PROGRAM_CACHE_KEY_TPL,
SITE_PATHWAY_IDS_CACHE_KEY_TPL,
......@@ -183,6 +184,27 @@ class TestGetPrograms(CacheIsolationTestCase):
self.assertEqual(actual_program, expected_program)
self.assertFalse(mock_warning.called)
def test_get_from_course(self, mock_warning, _mock_info):
expected_program = ProgramFactory()
expected_course = expected_program['courses'][0]['course_runs'][0]['key']
self.assertEqual(get_programs(course=expected_course), [])
cache.set(
COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_run_id=expected_course),
[expected_program['uuid']],
None
)
cache.set(
PROGRAM_CACHE_KEY_TPL.format(uuid=expected_program['uuid']),
expected_program,
None
)
actual_program = get_programs(course=expected_course)
self.assertEqual(actual_program, [expected_program])
self.assertFalse(mock_warning.called)
@skip_unless_lms
@mock.patch(UTILS_MODULE + '.logger.info')
......
......@@ -107,7 +107,9 @@ def get_programs(site=None, uuid=None, course=None): # pylint: disable=redefine
elif course:
uuids = cache.get(COURSE_PROGRAMS_CACHE_KEY_TPL.format(course_run_id=course))
if not uuids:
logger.warning(missing_details_msg_tpl.format(course=course))
# Currently, the cache does not differentiate between a cache miss and a course
# without programs. After this is changed, log any cache misses here.
return []
else:
uuids = cache.get(SITE_PROGRAM_UUIDS_CACHE_KEY_TPL.format(domain=site.domain), [])
if not uuids:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment