Commit bec6971a by Bill DeRusha

Monkey patch django.db.models.options for faster cache expiry

parent bd4cc57b
...@@ -9,7 +9,7 @@ settings.INSTALLED_APPS # pylint: disable=pointless-statement ...@@ -9,7 +9,7 @@ settings.INSTALLED_APPS # pylint: disable=pointless-statement
from openedx.core.lib.django_startup import autostartup from openedx.core.lib.django_startup import autostartup
import django import django
from monkey_patch import third_party_auth from monkey_patch import third_party_auth, django_db_models_options
import xmodule.x_module import xmodule.x_module
import cms.lib.xblock.runtime import cms.lib.xblock.runtime
...@@ -22,6 +22,7 @@ def run(): ...@@ -22,6 +22,7 @@ def run():
Executed during django startup Executed during django startup
""" """
third_party_auth.patch() third_party_auth.patch()
django_db_models_options.patch()
# Comprehensive theming needs to be set up before django startup, # Comprehensive theming needs to be set up before django startup,
# because modifying django template paths after startup has no effect. # because modifying django template paths after startup has no effect.
......
"""
Monkey patch implementation of the following _expire_cache performance improvement:
https://github.com/django/django/commit/7628f87e2b1ab4b8a881f06c8973be4c368aaa3d
Remove once we upgrade to a version of django which includes this fix natively!
NOTE: This is on django's master branch but is NOT currently part of any django 1.8 or 1.9 release.
"""
from django.db.models.options import Options
def patch():
"""
Monkey-patch the Options class.
"""
def _expire_cache(self, forward=True, reverse=True):
# pylint: disable=missing-docstring
# This method is usually called by apps.cache_clear(), when the
# registry is finalized, or when a new field is added.
if forward:
for cache_key in self.FORWARD_PROPERTIES:
if cache_key in self.__dict__:
delattr(self, cache_key)
if reverse and not self.abstract:
for cache_key in self.REVERSE_PROPERTIES:
if cache_key in self.__dict__:
delattr(self, cache_key)
self._get_fields_cache = {} # pylint: disable=protected-access
# Patch constants as a set instead of a list.
Options.FORWARD_PROPERTIES = {'fields', 'many_to_many', 'concrete_fields',
'local_concrete_fields', '_forward_fields_map'}
Options.REVERSE_PROPERTIES = {'related_objects', 'fields_map', '_relation_tree'}
# Patch the expire_cache method to utilize constant's new set data structure.
Options._expire_cache = _expire_cache # pylint: disable=protected-access
...@@ -12,7 +12,7 @@ from openedx.core.lib.django_startup import autostartup ...@@ -12,7 +12,7 @@ from openedx.core.lib.django_startup import autostartup
import edxmako import edxmako
import logging import logging
import analytics import analytics
from monkey_patch import third_party_auth from monkey_patch import third_party_auth, django_db_models_options
import xmodule.x_module import xmodule.x_module
...@@ -29,6 +29,7 @@ def run(): ...@@ -29,6 +29,7 @@ def run():
Executed during django startup Executed during django startup
""" """
third_party_auth.patch() third_party_auth.patch()
django_db_models_options.patch()
# To override the settings before executing the autostartup() for python-social-auth # To override the settings before executing the autostartup() for python-social-auth
if settings.FEATURES.get('ENABLE_THIRD_PARTY_AUTH', False): if settings.FEATURES.get('ENABLE_THIRD_PARTY_AUTH', False):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment