o
    ·„j½$  ã                   @   s–   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deeƒZdS )ai  
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 counterintuitive, 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.

* If the request had an Authorization header and the response was not marked
  "Cache-Control: public", the response will vary on Authorization.
é    N)Úsettings)ÚDEFAULT_CACHE_ALIASÚcaches)Úget_cache_keyÚget_max_ageÚhas_vary_headerÚlearn_cache_keyÚpatch_response_headersÚpatch_vary_headers)ÚMiddlewareMixin)Úparse_http_date_safec                       s<   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Z‡  Z	S )
Ú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.
    c                    s.   t ƒ  |¡ tj| _d | _tj| _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_alias©ÚselfÚget_response©Ú	__class__© úU/var/www/html/django_cbt/venv/lib/python3.10/site-packages/django/middleware/cache.pyr   J   s
   zUpdateCacheMiddleware.__init__c                 C   ó
   t | j S r   ©r   r   ©r   r   r   r   ÚcacheQ   ó   
zUpdateCacheMiddleware.cachec                 C   s   t |dƒo|jS )NÚ_cache_update_cache)Úhasattrr$   ©r   ÚrequestÚresponser   r   r   Ú_should_update_cacheU   s   z*UpdateCacheMiddleware._should_update_cachec                    s4  ˆ  ||¡s|S |js|jdvr|S |jrt|dƒr|S | dd¡ ¡ ‰ ˆ r3t‡ fdd„dD ƒƒr3|S t|dƒr:|S ˆj‰ˆd	u rSt	|ƒ‰ˆd	u rMˆj
‰nˆd
krS|S t|ˆƒ |j d¡rgdˆ vrgt|dƒ ˆr˜|jdkr˜t||ˆˆjˆjd�‰t|dƒr�t|jƒr�| ‡‡‡fdd„¡ |S ˆj ˆ|ˆ¡ |S )zSet the cache, if needed.)éÈ   i0  ÚCookiezCache-ControlÚ c                 3   s   � | ]}|ˆ v V  qd S r   r   )Ú.0Ú	directive)Úcache_controlr   r   Ú	<genexpr>i   s
   € ÿ
ÿz9UpdateCacheMiddleware.process_response.<locals>.<genexpr>)Úprivatezno-cachezno-storeÚ*Nr   ÚAuthorizationÚpublic)r3   r*   ©r"   Úrenderc                    s   ˆj  ˆ | ˆ¡S r   )r"   Úset)Úr)Ú	cache_keyr   Útimeoutr   r   Ú<lambda>�   s    z8UpdateCacheMiddleware.process_response.<locals>.<lambda>)r)   Ú	streamingÚstatus_codeÚcookiesr   ÚgetÚlowerÚanyr   r   r   r	   Úheadersr
   r   r   r"   r%   Úcallabler6   Úadd_post_render_callbackr7   r&   r   )r/   r9   r   r:   r   Úprocess_responseX   sD   þ


ÿÿÿz&UpdateCacheMiddleware.process_response)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr"   r)   rE   Ú__classcell__r   r   r   r   r   @   s    	
r   c                       s4   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Z‡  ZS )Ú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.
    c                    s    t ƒ  |¡ tj| _tj| _d S r   )r   r   r   r   r   r   r   r   r   r   r   r       s   z!FetchFromCacheMiddleware.__init__c                 C   r   r   r    r!   r   r   r   r"   ¥   r#   zFetchFromCacheMiddleware.cachec                 C   sÞ   |j dvr
d|_dS t|| jd| jd�}|du rd|_dS | j |¡}|du r<|j dkr<t|| jd| jd�}| j |¡}|du rEd|_dS t|ƒ }durjt|d ƒ }durjtt	 	¡ ƒ}|| }t
d	|| ƒ|d
< d|_|S )zn
        Check whether the page is already cached and return the cached
        version if available.
        )ÚGETÚHEADFNrM   r5   TrN   ÚExpiresr   ÚAge)Úmethodr$   r   r   r"   r?   r   r   ÚintÚtimeÚmax)r   r'   r9   r(   Úmax_age_secondsÚexpires_timestampÚnow_timestampÚremaining_secondsr   r   r   Úprocess_request©   s2   
ÿþz(FetchFromCacheMiddleware.process_request)	rF   rG   rH   rI   r   rJ   r"   rY   rK   r   r   r   r   rL   —   s    
rL   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚCacheMiddlewarezÌ
    Cache middleware that provides basic behavior for many simple sites.

    Also used as the hook point for the cache decorator, which is generated
    using the decorator-from-middleware utility.
    Nc                    sˆ   t ƒ  |¡ z|d }|d u rd}|| _W n	 ty   Y nw z|d }|d u r*t}|| _W n	 ty7   Y nw |d ur?|| _|| _d S )Nr   r,   r   )r   r   r   ÚKeyErrorr   r   r   r   )r   r   r   r   Úkwargsr   r   r   r   r   r   Ù   s(   
ÿ
ÿ
zCacheMiddleware.__init__)NN)rF   rG   rH   rI   r   rK   r   r   r   r   rZ   Ñ   s    rZ   )rI   rS   Údjango.confr   Údjango.core.cacher   r   Údjango.utils.cacher   r   r   r   r	   r
   Údjango.utils.deprecationr   Údjango.utils.httpr   r   rL   rZ   r   r   r   r   Ú<module>   s    / W: