Cache org-site lookups in the RequestCache

This commit is contained in:
Calen Pennington
2019-01-22 21:21:27 -05:00
parent a3541d6e46
commit a842921e1f
5 changed files with 98 additions and 25 deletions

View File

@@ -22,6 +22,7 @@ import crum
from config_models.models import ConfigurationModel, cache
from openedx.core.djangoapps.site_configuration.models import SiteConfiguration
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.lib.cache_utils import request_cached
class Provenance(Enum):
@@ -252,7 +253,9 @@ class StackedConfigurationModel(ConfigurationModel):
return course_key.org
@classmethod
@request_cached()
def _site_from_org(cls, org):
configuration = SiteConfiguration.get_configuration_for_org(org, select_related=['site'])
if configuration is None:
try:

View File

@@ -9,6 +9,7 @@ import zlib
from django.utils.encoding import force_text
from edx_django_utils.cache import RequestCache
import wrapt
def request_cached(namespace=None, arg_map_function=None, request_cache_getter=None):
@@ -47,38 +48,32 @@ def request_cached(namespace=None, arg_map_function=None, request_cache_getter=N
cache the value it returns, and return that cached value for subsequent calls with the
same args/kwargs within a single request.
"""
def decorator(f):
@wrapt.decorator
def decorator(wrapped, instance, args, kwargs):
"""
Arguments:
f (func): the function to wrap
args, kwargs: values passed into the wrapped function
"""
@functools.wraps(f)
def _decorator(*args, **kwargs):
"""
Arguments:
args, kwargs: values passed into the wrapped function
"""
# Check to see if we have a result in cache. If not, invoke our wrapped
# function. Cache and return the result to the caller.
if request_cache_getter:
request_cache = request_cache_getter(args, kwargs)
else:
request_cache = RequestCache(namespace)
# Check to see if we have a result in cache. If not, invoke our wrapped
# function. Cache and return the result to the caller.
if request_cache_getter:
request_cache = request_cache_getter(args if instance is None else (instance,) + args, kwargs)
else:
request_cache = RequestCache(namespace)
if request_cache:
cache_key = _func_call_cache_key(f, arg_map_function, *args, **kwargs)
cached_response = request_cache.get_cached_response(cache_key)
if cached_response.is_found:
return cached_response.value
if request_cache:
cache_key = _func_call_cache_key(wrapped, arg_map_function, *args, **kwargs)
cached_response = request_cache.get_cached_response(cache_key)
if cached_response.is_found:
return cached_response.value
result = f(*args, **kwargs)
result = wrapped(*args, **kwargs)
if request_cache:
request_cache.set(cache_key, result)
if request_cache:
request_cache.set(cache_key, result)
return result
return result
return _decorator
return decorator