1
0
mirror of https://github.com/django/django.git synced 2025-08-12 21:09:13 +00:00

Simplified caching of templatetags modules.

This commit is contained in:
Aymeric Augustin 2014-11-18 21:50:52 +01:00
parent e23240474b
commit a97e72aaab
3 changed files with 16 additions and 25 deletions

View File

@ -10,6 +10,7 @@ from django.apps import apps
from django.conf import settings from django.conf import settings
from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility from django.template.context import (BaseContext, Context, RequestContext, # NOQA: imported for backwards compatibility
ContextPopException) ContextPopException)
from django.utils import lru_cache
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.itercompat import is_iterable from django.utils.itercompat import is_iterable
from django.utils.text import (smart_split, unescape_string_literal, from django.utils.text import (smart_split, unescape_string_literal,
@ -1296,32 +1297,27 @@ def import_library(taglib_module):
"a variable named 'register'" % "a variable named 'register'" %
taglib_module) taglib_module)
templatetags_modules = []
@lru_cache.lru_cache()
def get_templatetags_modules(): def get_templatetags_modules():
""" """
Return the list of all available template tag modules. Return the list of all available template tag modules.
Caches the result for faster access. Caches the result for faster access.
""" """
global templatetags_modules templatetags_modules_candidates = ['django.templatetags']
if not templatetags_modules: templatetags_modules_candidates += [
_templatetags_modules = [] '%s.templatetags' % app_config.name
# Populate list once per process. Mutate the local list first, and for app_config in apps.get_app_configs()]
# then assign it to the global name to ensure there are no cases where
# two threads try to populate it simultaneously.
templatetags_modules_candidates = ['django.templatetags'] templatetags_modules = []
templatetags_modules_candidates += ['%s.templatetags' % app_config.name for templatetag_module in templatetags_modules_candidates:
for app_config in apps.get_app_configs()] try:
for templatetag_module in templatetags_modules_candidates: import_module(templatetag_module)
try: except ImportError:
import_module(templatetag_module) continue
_templatetags_modules.append(templatetag_module) else:
except ImportError: templatetags_modules.append(templatetag_module)
continue
templatetags_modules = _templatetags_modules
return templatetags_modules return templatetags_modules

View File

@ -37,8 +37,8 @@ def update_installed_apps(**kwargs):
from django.core.management import get_commands from django.core.management import get_commands
get_commands.cache_clear() get_commands.cache_clear()
# Rebuild templatetags module cache. # Rebuild templatetags module cache.
from django.template import base as mod from django.template.base import get_templatetags_modules
mod.templatetags_modules = [] get_templatetags_modules.cache_clear()
# Rebuild get_app_template_dirs cache. # Rebuild get_app_template_dirs cache.
from django.template.utils import get_app_template_dirs from django.template.utils import get_app_template_dirs
get_app_template_dirs.cache_clear() get_app_template_dirs.cache_clear()

View File

@ -1855,11 +1855,6 @@ class TemplateTagLoading(TestCase):
def setUp(self): def setUp(self):
self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__)) self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
self.old_tag_modules = template_base.templatetags_modules
template_base.templatetags_modules = []
def tearDown(self):
template_base.templatetags_modules = self.old_tag_modules
def test_load_error(self): def test_load_error(self):
ttext = "{% load broken_tag %}" ttext = "{% load broken_tag %}"