gpt4 book ai didi

django - 清除某些应用程序或页面的 Django 缓存

转载 作者:行者123 更新时间:2023-12-04 20:53:43 26 4
gpt4 key购买 nike

是否有可能清除 django 中某些应用程序或某些页面的缓存?
我试图找到它,但徒劳无功。

提前致谢。

最佳答案

像这样的东西可能会帮助你..

    from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
from django.utils.cache import get_cache_key, _generate_cache_header_key, _generate_cache_key
from django.core.urlresolvers import reverse
from django.http import HttpRequest
from django.conf import settings

def expire_cache(path, args=[], cache_name=None, isview=True, lang_code=None, method='GET'):
if cache_name is None:
cache_name = DEFAULT_CACHE_ALIAS

cache = get_cache(cache_name)
key_prefix = settings.CACHES[cache_name].get('KEY_PREFIX', '')

request = HttpRequest()
if isview:
request.path = reverse(path, args=args)
else:
request.path = path

language_code = lang_code or getattr(settings, 'LANGUAGE_CODE')
if language_code:
request.LANGUAGE_CODE = language_code

header_key = _generate_cache_header_key(key_prefix, request)

if not header_key:
return False

headerlist = cache.get(header_key, None)
if headerlist is not None:
cache.set(header_key, None, 0)
page_key = _generate_cache_key(request, method, headerlist, key_prefix)

if not page_key:
return False

cache.set(page_key, None, 0)
return True

expire_cache('apps.yourapp.views.function')

关于django - 清除某些应用程序或页面的 Django 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7257953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com