测试gitnore
This commit is contained in:
@@ -46,10 +46,7 @@ More details about how the caching works:
|
||||
from django.conf import settings
|
||||
from django.core.cache import DEFAULT_CACHE_ALIAS, caches
|
||||
from django.utils.cache import (
|
||||
get_cache_key,
|
||||
get_max_age,
|
||||
has_vary_header,
|
||||
learn_cache_key,
|
||||
get_cache_key, get_max_age, has_vary_header, learn_cache_key,
|
||||
patch_response_headers,
|
||||
)
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
@@ -64,8 +61,9 @@ class UpdateCacheMiddleware(MiddlewareMixin):
|
||||
UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE
|
||||
so that it'll get called last during the response phase.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
# RemovedInDjango40Warning: when the deprecation ends, replace with:
|
||||
# def __init__(self, get_response):
|
||||
def __init__(self, get_response=None):
|
||||
super().__init__(get_response)
|
||||
self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
|
||||
self.page_timeout = None
|
||||
@@ -74,7 +72,7 @@ class UpdateCacheMiddleware(MiddlewareMixin):
|
||||
self.cache = caches[self.cache_alias]
|
||||
|
||||
def _should_update_cache(self, request, response):
|
||||
return hasattr(request, "_cache_update_cache") and request._cache_update_cache
|
||||
return hasattr(request, '_cache_update_cache') and request._cache_update_cache
|
||||
|
||||
def process_response(self, request, response):
|
||||
"""Set the cache, if needed."""
|
||||
@@ -87,15 +85,11 @@ class UpdateCacheMiddleware(MiddlewareMixin):
|
||||
|
||||
# Don't cache responses that set a user-specific (and maybe security
|
||||
# sensitive) cookie in response to a cookie-less request.
|
||||
if (
|
||||
not request.COOKIES
|
||||
and response.cookies
|
||||
and has_vary_header(response, "Cookie")
|
||||
):
|
||||
if not request.COOKIES and response.cookies and has_vary_header(response, 'Cookie'):
|
||||
return response
|
||||
|
||||
# Don't cache a response with 'Cache-Control: private'
|
||||
if "private" in response.get("Cache-Control", ()):
|
||||
if 'private' in response.get('Cache-Control', ()):
|
||||
return response
|
||||
|
||||
# Page timeout takes precedence over the "max-age" and the default
|
||||
@@ -112,10 +106,8 @@ class UpdateCacheMiddleware(MiddlewareMixin):
|
||||
return response
|
||||
patch_response_headers(response, timeout)
|
||||
if timeout and response.status_code == 200:
|
||||
cache_key = learn_cache_key(
|
||||
request, response, timeout, self.key_prefix, cache=self.cache
|
||||
)
|
||||
if hasattr(response, "render") and callable(response.render):
|
||||
cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
|
||||
if hasattr(response, 'render') and callable(response.render):
|
||||
response.add_post_render_callback(
|
||||
lambda r: self.cache.set(cache_key, r, timeout)
|
||||
)
|
||||
@@ -132,8 +124,9 @@ class FetchFromCacheMiddleware(MiddlewareMixin):
|
||||
FetchFromCacheMiddleware must be the last piece of middleware in MIDDLEWARE
|
||||
so that it'll get called last during the request phase.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
# RemovedInDjango40Warning: when the deprecation ends, replace with:
|
||||
# def __init__(self, get_response):
|
||||
def __init__(self, get_response=None):
|
||||
super().__init__(get_response)
|
||||
self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
|
||||
self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
|
||||
@@ -144,21 +137,19 @@ class FetchFromCacheMiddleware(MiddlewareMixin):
|
||||
Check whether the page is already cached and return the cached
|
||||
version if available.
|
||||
"""
|
||||
if request.method not in ("GET", "HEAD"):
|
||||
if request.method not in ('GET', 'HEAD'):
|
||||
request._cache_update_cache = False
|
||||
return None # Don't bother checking the cache.
|
||||
|
||||
# try and get the cached GET response
|
||||
cache_key = get_cache_key(request, self.key_prefix, "GET", cache=self.cache)
|
||||
cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
|
||||
if cache_key is None:
|
||||
request._cache_update_cache = True
|
||||
return None # No cache information available, need to rebuild.
|
||||
response = self.cache.get(cache_key)
|
||||
# if it wasn't found and we are looking for a HEAD, try looking just for that
|
||||
if response is None and request.method == "HEAD":
|
||||
cache_key = get_cache_key(
|
||||
request, self.key_prefix, "HEAD", cache=self.cache
|
||||
)
|
||||
if response is None and request.method == 'HEAD':
|
||||
cache_key = get_cache_key(request, self.key_prefix, 'HEAD', cache=self.cache)
|
||||
response = self.cache.get(cache_key)
|
||||
|
||||
if response is None:
|
||||
@@ -177,8 +168,9 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
|
||||
Also used as the hook point for the cache decorator, which is generated
|
||||
using the decorator-from-middleware utility.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response, cache_timeout=None, page_timeout=None, **kwargs):
|
||||
# RemovedInDjango40Warning: when the deprecation ends, replace with:
|
||||
# def __init__(self, get_response, cache_timeout=None, page_timeout=None, **kwargs):
|
||||
def __init__(self, get_response=None, cache_timeout=None, page_timeout=None, **kwargs):
|
||||
super().__init__(get_response)
|
||||
# We need to differentiate between "provided, but using default value",
|
||||
# and "not provided". If the value is provided using a default, then
|
||||
@@ -186,14 +178,14 @@ class CacheMiddleware(UpdateCacheMiddleware, FetchFromCacheMiddleware):
|
||||
# we need to use middleware defaults.
|
||||
|
||||
try:
|
||||
key_prefix = kwargs["key_prefix"]
|
||||
key_prefix = kwargs['key_prefix']
|
||||
if key_prefix is None:
|
||||
key_prefix = ""
|
||||
key_prefix = ''
|
||||
self.key_prefix = key_prefix
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
cache_alias = kwargs["cache_alias"]
|
||||
cache_alias = kwargs['cache_alias']
|
||||
if cache_alias is None:
|
||||
cache_alias = DEFAULT_CACHE_ALIAS
|
||||
self.cache_alias = cache_alias
|
||||
|
||||
Reference in New Issue
Block a user