From 1d32019e052d970fda1e131c95398324ff0fdfc4 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Sat, 18 Jan 2025 12:35:03 -0500 Subject: [PATCH] perf: reduce StackedConfigurationModel cache queries TieredCache is a hybrid request cache + regular cache, where we check against a Django cache for the first get, but then store that data in a process-local request cache. ConfigurationModels were already converted to use TieredCache for performance reasons, but we never converted the StackedConfigurationModel subclass. This can reduce the lookups for ContentTypeGatingConfig by hundreds of network cache calls in a given request (depending on how many exams are in a course). This also affects the following subclasses of StackedConfigurationModel, though I have not measured the precise effects: * CourseDurationLimitConfig * DisableProgressPageStackedConfig * DiscountRestrictionConfig * DiscountPercentageConfig * ProviderFilter (for dicusssions providers) * SelfPacedRelativeDatesConfig --- .../core/djangoapps/config_model_utils/models.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/openedx/core/djangoapps/config_model_utils/models.py b/openedx/core/djangoapps/config_model_utils/models.py index 11d4d6383a10..629fdeed8435 100644 --- a/openedx/core/djangoapps/config_model_utils/models.py +++ b/openedx/core/djangoapps/config_model_utils/models.py @@ -11,7 +11,7 @@ from enum import Enum import crum -from config_models.models import ConfigurationModel, cache +from config_models.models import ConfigurationModel from django.conf import settings from django.contrib.sites.models import Site from django.contrib.sites.requests import RequestSite @@ -19,6 +19,7 @@ from django.db import models from django.db.models import Q from django.utils.translation import gettext_lazy as _ +from edx_django_utils.cache.utils import TieredCache from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.djangoapps.site_configuration.models import SiteConfiguration @@ -49,6 +50,8 @@ class StackedConfigurationModel(ConfigurationModel): """ A ConfigurationModel that stacks Global, Site, Org, Course, and Course Run level configuration values. + + use tieredcache: https://github.com/openedx/django-config-models/blob/32c885d92131fcac1680c518c6f8a127a67f5e2e/config_models/models.py """ class Meta: abstract = True @@ -157,10 +160,9 @@ def current(cls, site=None, org=None, org_course=None, course_key=None): # pyli no arguments are supplied). """ cache_key_name = cls.cache_key_name(site, org, org_course, course_key) - cached = cache.get(cache_key_name) - - if cached is not None: - return cached + cached_response = TieredCache.get_cached_response(cache_key_name) + if cached_response.is_found and cached_response.value is not None: + return cached_response.value # Raise an error if more than one of site/org/course are specified simultaneously. if len([arg for arg in [site, org, org_course, course_key] if arg is not None]) > 1: @@ -235,7 +237,8 @@ def sort_key(override): current = cls(**values) current.provenances = {field.name: provenances[field.name] for field in stackable_fields} # pylint: disable=attribute-defined-outside-init - cache.set(cache_key_name, current, cls.cache_timeout) + + TieredCache.set_all_tiers(cache_key_name, current, cls.cache_timeout) return current @classmethod