Skip to content

Commit

Permalink
perf: reduce StackedConfigurationModel cache queries
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ormsbee committed Jan 18, 2025
1 parent 4035aa3 commit cacf861
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions openedx/core/djangoapps/config_model_utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
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
from django.core.exceptions import ValidationError
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
Expand Down Expand Up @@ -157,10 +158,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:
Expand Down Expand Up @@ -235,7 +235,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
Expand Down

0 comments on commit cacf861

Please sign in to comment.