

_V                 @   s   d  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 Gd d   d e  Z Gd d   d e  Z Gd	 d
   d
 e e  Z d S)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_CLASSES = [
        '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_headersc               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	UpdateCacheMiddlewarea>  
    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_CLASSES so that it'll get called last during the response phase.
    c             C   s8   t  j |  _ t  j |  _ t  j |  _ t |  j |  _ d  S)N)	r   CACHE_MIDDLEWARE_SECONDScache_timeoutCACHE_MIDDLEWARE_KEY_PREFIX
key_prefixCACHE_MIDDLEWARE_ALIAScache_aliasr   cache)self r   9/tmp/pip-build-ghmbqnp_/Django/django/middleware/cache.py__init__?   s    zUpdateCacheMiddleware.__init__c             C   s   t  | d  o | j S)N_cache_update_cache)hasattrr   )r   requestresponser   r   r   _should_update_cacheE   s    z*UpdateCacheMiddleware._should_update_cachec                s   j  | |  s | S| j s. | j d k r2 | S| j rX | j rX t | d  rX | St |    d k r|  j  n  d k r | St |    rt	 | |   j
 d  j   t | d  r t | j  r | j     f d d    q j j   |   n  | S)	zSets the cache, if needed.   CookieNr   r   renderc                s    j  j   |    S)N)r   set)r)	cache_keyr   timeoutr   r   <lambda>d   s    z8UpdateCacheMiddleware.process_response.<locals>.<lambda>)r   Z	streamingstatus_codeZCOOKIEScookiesr   r   r   r	   r   r   r   r   callabler   Zadd_post_render_callbackr   )r   r   r   r   )r    r   r!   r   process_responseH   s&    "!z&UpdateCacheMiddleware.process_responseN)__name__
__module____qualname____doc__r   r   r&   r   r   r   r   r
   6   s   r
   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d 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_CLASSES so that it'll get called last during the request phase.
    c             C   s,   t  j |  _ t  j |  _ t |  j |  _ d  S)N)r   r   r   r   r   r   r   )r   r   r   r   r   s   s    z!FetchFromCacheMiddleware.__init__c             C   s   | j  d k r d | _ d St | |  j d d |  j } | d k rS d | _ d S|  j j |  } | d k r | j  d k r t | |  j d d |  j } |  j j |  } n  | d k r d | _ d Sd | _ | S)zp
        Checks whether the page is already cached and returns the cached
        version if available.
        GETHEADFNr   T)zGETzHEAD)methodr   r   r   r   get)r   r   r    r   r   r   r   process_requestx   s     				z(FetchFromCacheMiddleware.process_requestN)r'   r(   r)   r*   r   r0   r   r   r   r   r+   k   s   r+   c               @   s%   e  Z d  Z d Z d d d  Z d 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             K   s   y# | d } | d  k r" d } n  Wn t  k
 r@ t j } Yn X| |  _ y# | d } | d  k rl t } n  Wn t  k
 r t j } Yn X| |  _ | d  k r t j } n  | |  _ t	 |  j |  _
 d  S)Nr    r   )KeyErrorr   r   r   r   r   r   r   r   r   r   )r   r   kwargsr   r   r   r   r   r      s$    
	
		zCacheMiddleware.__init__)r'   r(   r)   r*   r   r   r   r   r   r1      s   r1   N)r*   Zdjango.confr   Zdjango.core.cacher   r   Zdjango.utils.cacher   r   r   r   r	   objectr
   r+   r1   r   r   r   r   <module>,   s   (5*