File: //usr/lib/python3/dist-packages/django/middleware/__pycache__/cache.cpython-310.pyc
o
3�a# � @ s~ d Z ddlmZ ddlmZmZ ddlmZmZm Z m
Z
mZ ddlm
Z
G dd� de
�ZG dd � d e
�ZG d
d� dee�ZdS )
a�
Cache middleware. If enabled, each Django-powered page will be cached based on
URL. The canonical way to enable cache middleware is to set
``UpdateCacheMiddleware`` as your first piece of middleware, and
``FetchFromCacheMiddleware`` as the last::
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware',
...
'django.middleware.cache.FetchFromCacheMiddleware'
]
This is counter-intuitive, but correct: ``UpdateCacheMiddleware`` needs to run
last during the response phase, which processes middleware bottom-up;
``FetchFromCacheMiddleware`` needs to run last during the request phase, which
processes middleware top-down.
The single-class ``CacheMiddleware`` can be used for some simple sites.
However, if any other piece of middleware needs to affect the cache key, you'll
need to use the two-part ``UpdateCacheMiddleware`` and
``FetchFromCacheMiddleware``. This'll most often happen when you're using
Django's ``LocaleMiddleware``.
More details about how the caching works:
* Only GET or HEAD-requests with status code 200 are cached.
* The number of seconds each page is stored for is set by the "max-age" section
of the response's "Cache-Control" header, falling back to the
CACHE_MIDDLEWARE_SECONDS setting if the section was not found.
* This middleware expects that a HEAD request is answered with the same response
headers exactly like the corresponding GET request.
* When a hit occurs, a shallow copy of the original response object is returned
from process_request.
* Pages will be cached based on the contents of the request headers listed in
the response's "Vary" header.
* This middleware also sets ETag, Last-Modified, Expires and Cache-Control
headers on the response object.
� )�settings)�DEFAULT_CACHE_ALIAS�caches)�
get_cache_key�get_max_age�has_vary_header�learn_cache_key�patch_response_headers)�MiddlewareMixinc s2 e Zd ZdZd � fdd� Zdd� Zdd� Z� ZS )
�UpdateCacheMiddlewarea6
Response-phase cache middleware that updates the cache if the response is
cacheable.
Must be used as part of the two-part update/fetch cache middleware.
UpdateCacheMiddleware must be the first piece of middleware in MIDDLEWARE
so that it'll get called last during the response phase.
Nc s: t � �|� tj| _d | _tj| _tj| _ t
| j | _d S �N)�super�__init__r �CACHE_MIDDLEWARE_SECONDS�
cache_timeout�page_timeout�CACHE_MIDDLEWARE_KEY_PREFIX�
key_prefix�CACHE_MIDDLEWARE_ALIAS�cache_aliasr �cache��self�get_response�� __class__� �9/usr/lib/python3/dist-packages/django/middleware/cache.pyr B s zUpdateCacheMiddleware.__init__c C s t |d�o|jS )N�_cache_update_cache)�hasattrr �r �request�responser r r �_should_update_cacheJ s z*UpdateCacheMiddleware._should_update_cachec s� �� ||�s|S |js|jdvr|S |js|jrt|d�r|S d|�dd�v r)|S �j��du rBt|���du r<�j �n�dkrB|S t
|�� �rx|jdkrxt||��j�j
d �� t|d
�rpt|j�rp|�� ��fdd�� |S �j
�� |�� |S )
zSet the cache, if needed.)�� i0 �Cookie�privatez
Cache-Controlr Nr r$ �r �renderc s �j �� | ��S r )r �set)�r�� cache_keyr �timeoutr r �<lambda>p s z8UpdateCacheMiddleware.process_response.<locals>.<lambda>)r# � streaming�status_code�COOKIES�cookiesr �getr r r r r r r r �callabler( �add_post_render_callbackr) r r r+ r �process_responseM s2
��z&UpdateCacheMiddleware.process_responser )�__name__�
__module__�__qualname__�__doc__r r# r6 �
__classcell__r r r r r 7 s
r c s* e Zd ZdZd� fdd� Zdd� Z� ZS )�FetchFromCacheMiddlewarea!
Request-phase cache middleware that fetches a page from the cache.
Must be used as part of the two-part update/fetch cache middleware.
FetchFromCacheMiddleware must be the last piece of middleware in MIDDLEWARE
so that it'll get called last during the request phase.
Nc s, t � �|� tj| _tj| _t| j | _d S r ) r
r r r r r r r r r r r r r � s z!FetchFromCacheMiddleware.__init__c C s� |j dvr
d|_dS t|| jd| jd�}|du rd|_dS | j�|�}|du r<|j dkr<t|| jd| jd�}| j�|�}|du rEd|_dS d|_|S )zn
Check whether the page is already cached and return the cached
version if available.
)�GET�HEADFNr= r'