Ticket #14290: l10n_performance.diff

File l10n_performance.diff, 2.5 KB (added by Teemu Kurppa <teemu.kurppa@…>, 14 years ago)

Patch to cache localization formats

  • django/utils/formats.py

     
    77from django.utils.encoding import smart_str
    88from django.utils import dateformat, numberformat, datetime_safe
    99
     10
     11# _cached_formats is a mapping from format_type to a 1-tuple. This allows
     12# us to separate 3 distinct cases:
     13#      (None,)            the format_type was checked previously, but
     14#                         no value was found.
     15#      (<format value>, ) the previously fetched value for format_type
     16#                         is format_value
     17#      if a format_type key is not in _cached_formats, then format_type
     18#      hasn't yet been fetched.
     19_cached_formats = {}
     20
    1021def get_format_modules(reverse=False):
    1122    """
    1223    Returns an iterator over the format modules found in the project and Django
     
    3445        modules.reverse()
    3546    return modules
    3647
     48
    3749def get_format(format_type):
    3850    """
    3951    For a specific format type, returns the format for the current
     
    4254    """
    4355    format_type = smart_str(format_type)
    4456    if settings.USE_L10N:
    45         for module in get_format_modules():
    46             try:
    47                 return getattr(module, format_type)
    48             except AttributeError:
     57        global _cached_formats
     58        cached_value = _cached_formats.get(format_type, None)
     59        if cached_value:
     60            if cached_value[0]:
     61                return cached_value[0]
     62            else:
     63                # format_type was previously checked, but didn't have a value
    4964                pass
     65        else:
     66            for module in get_format_modules():               
     67                try:
     68                    format_value = getattr(module, format_type)
     69                    _cached_formats[format_type] = (format_value,)
     70                    return format_value
     71                except AttributeError:
     72                    pass
     73            _cached_formats[format_type] = (None,)
    5074    return getattr(settings, format_type)
    5175
    5276def date_format(value, format=None):
  • AUTHORS

     
    276276    knox <christobzr@gmail.com>
    277277    David Krauth
    278278    Kevin Kubasik <kevin@kubasik.net>
     279    Teemu Kurppa <http://dirtyaura.org>
    279280    kurtiss@meetro.com
    280281    Denis Kuzmichyov <kuzmichyov@gmail.com>
    281282    Panos Laganakos <panos.laganakos@gmail.com>
Back to Top