本文整理汇总了Python中django.utils.lru_cache.lru_cache方法的典型用法代码示例。如果您正苦于以下问题:Python lru_cache.lru_cache方法的具体用法?Python lru_cache.lru_cache怎么用?Python lru_cache.lru_cache使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.lru_cache
的用法示例。
在下文中一共展示了lru_cache.lru_cache方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_for_language
# 需要导入模块: from django.utils import lru_cache [as 别名]
# 或者: from django.utils.lru_cache import lru_cache [as 别名]
def check_for_language(lang_code):
"""
Checks whether there is a global language file for the given language
code. This is used to decide whether a user-provided language is
available.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
if lang_code is None or not language_code_re.search(lang_code):
return False
for path in all_locale_paths():
if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
return True
return False
示例2: reset_hashers
# 需要导入模块: from django.utils import lru_cache [as 别名]
# 或者: from django.utils.lru_cache import lru_cache [as 别名]
def reset_hashers(self):
"""
Wrapper to manually reset django's hasher lookup cache
"""
# resets cache for .get_hashers() & .get_hashers_by_algorithm()
from django.contrib.auth.hashers import reset_hashers
reset_hashers(setting="PASSWORD_HASHERS")
# reset internal caches
super(DjangoContextAdapter, self).reset_hashers()
#=============================================================================
# django hashers helpers -- hasher lookup
#=============================================================================
# lru_cache()'ed by init
示例3: get_supported_language_variant
# 需要导入模块: from django.utils import lru_cache [as 别名]
# 或者: from django.utils.lru_cache import lru_cache [as 别名]
def get_supported_language_variant(lang_code, strict=False):
"""
Returns the language-code that's listed in supported languages, possibly
selecting a more generic variant. Raises LookupError if nothing found.
If `strict` is False (the default), the function will look for an alternative
country-specific variant when the currently checked is not found.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
if lang_code:
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
possible_lang_codes = [lang_code]
try:
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
except KeyError:
pass
generic_lang_code = lang_code.split('-')[0]
possible_lang_codes.append(generic_lang_code)
supported_lang_codes = get_languages()
for code in possible_lang_codes:
if code in supported_lang_codes and check_for_language(code):
return code
if not strict:
# if fr-fr is not supported, try fr-ca.
for supported_code in supported_lang_codes:
if supported_code.startswith(generic_lang_code + '-'):
return supported_code
raise LookupError(lang_code)
示例4: __init__
# 需要导入模块: from django.utils import lru_cache [as 别名]
# 或者: from django.utils.lru_cache import lru_cache [as 别名]
def __init__(self, context=None, get_user_category=None, **kwds):
# init log
self.log = logging.getLogger(__name__ + ".DjangoContextAdapter")
# init parent, filling in default context object
if context is None:
context = CryptContext()
super(DjangoContextAdapter, self).__init__(context=context, **kwds)
# setup user category
if get_user_category:
assert callable(get_user_category)
self.get_user_category = get_user_category
# install lru cache wrappers
from django.utils.lru_cache import lru_cache
self.get_hashers = lru_cache()(self.get_hashers)
# get copy of original make_password
from django.contrib.auth.hashers import make_password
if make_password.__module__.startswith("passlib."):
make_password = _PatchManager.peek_unpatched_func(make_password)
self._orig_make_password = make_password
# get other django helpers
from django.contrib.auth.hashers import is_password_usable
self.is_password_usable = is_password_usable
# init manager
mlog = logging.getLogger(__name__ + ".DjangoContextAdapter._manager")
self._manager = _PatchManager(log=mlog)
示例5: ignore_unhashable_lru_cache
# 需要导入模块: from django.utils import lru_cache [as 别名]
# 或者: from django.utils.lru_cache import lru_cache [as 别名]
def ignore_unhashable_lru_cache(maxsize: int=128, typed: bool=False) -> DECORATOR:
"""
This is a wrapper over lru_cache function. It adds following features on
top of lru_cache:
* It will not cache result of functions with unhashable arguments.
* It will clear cache whenever zerver.lib.cache.KEY_PREFIX changes.
"""
internal_decorator = lru_cache(maxsize=maxsize, typed=typed)
def decorator(user_function: Callable[..., Any]) -> Callable[..., Any]:
if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage
# In the development environment, we want every file
# change to refresh the source files from disk.
return user_function
cache_enabled_user_function = internal_decorator(user_function)
def wrapper(*args: Any, **kwargs: Any) -> Any:
if not hasattr(cache_enabled_user_function, 'key_prefix'):
cache_enabled_user_function.key_prefix = KEY_PREFIX
if cache_enabled_user_function.key_prefix != KEY_PREFIX:
# Clear cache when cache.KEY_PREFIX changes. This is used in
# tests.
cache_enabled_user_function.cache_clear()
cache_enabled_user_function.key_prefix = KEY_PREFIX
try:
return cache_enabled_user_function(*args, **kwargs)
except TypeError:
# args or kwargs contains an element which is unhashable. In
# this case we don't cache the result.
pass
# Deliberately calling this function from outside of exception
# handler to get a more descriptive traceback. Otherwise traceback
# can include the exception from cached_enabled_user_function as
# well.
return user_function(*args, **kwargs)
setattr(wrapper, 'cache_info', cache_enabled_user_function.cache_info)
setattr(wrapper, 'cache_clear', cache_enabled_user_function.cache_clear)
return wrapper
return decorator