ó
ù¢TQc           @   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
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e e f d
 „  ƒ  YZ d S(   sæ  
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.

* If CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests
  (i.e., those not made by a logged-in user) will be cached. This is a simple
  and effective way of avoiding the caching of the Django admin (and any other
  user-specific content).

* 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.

iÿÿÿÿ(   t   settings(   t	   get_cachet   DEFAULT_CACHE_ALIAS(   t   get_cache_keyt   learn_cache_keyt   patch_response_headerst   get_max_aget   UpdateCacheMiddlewarec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s>  
    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   sO   t  j |  _ t  j |  _ t t  d t ƒ |  _ t  j |  _	 t
 |  j	 ƒ |  _ d  S(   Nt   CACHE_MIDDLEWARE_ANONYMOUS_ONLY(   R    t   CACHE_MIDDLEWARE_SECONDSt   cache_timeoutt   CACHE_MIDDLEWARE_KEY_PREFIXt
   key_prefixt   getattrt   Falset   cache_anonymous_onlyt   CACHE_MIDDLEWARE_ALIASt   cache_aliasR   t   cache(   t   self(    (    s0   ../Django//lib/python/django/middleware/cache.pyt   __init__A   s
    c         C   s'   y | j  j SWn t k
 r" t SXd  S(   N(   t   sessiont   accessedt   AttributeErrorR   (   R   t   request(    (    s0   ../Django//lib/python/django/middleware/cache.pyt   _session_accessedH   s    c         C   sk   t  | d ƒ s | j r t S|  j rg |  j | ƒ rg t  | d ƒ sQ t d ƒ ‚ | j j ƒ  rg t Sn  t S(   Nt   _cache_update_cachet   usersþ   The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware.(	   t   hasattrR   R   R   R   t   AssertionErrorR   t   is_authenticatedt   True(   R   R   t   response(    (    s0   ../Django//lib/python/django/middleware/cache.pyt   _should_update_cacheN   s    c            sô   ˆ  j  | | ƒ s | S| j s. | j d k r2 | St | ƒ ‰ ˆ d k rV ˆ  j ‰ n ˆ d k rf | St | ˆ ƒ ˆ rð t | | ˆ ˆ  j d ˆ  j	 ƒ‰ t
 | d ƒ r× t | j ƒ r× | j ‡ ‡  ‡ f d †  ƒ qð ˆ  j	 j ˆ | ˆ ƒ n  | S(   s   Sets the cache, if needed.iÈ   i    R   t   renderc            s   ˆ j  j ˆ  |  ˆ ƒ S(   N(   R   t   set(   t   r(   t	   cache_keyR   t   timeout(    s0   ../Django//lib/python/django/middleware/cache.pyt   <lambda>p   s    N(   R!   t	   streamingt   status_codeR   t   NoneR
   R   R   R   R   R   t   callableR"   t   add_post_render_callbackR#   (   R   R   R    (    (   R   R&   R%   s0   ../Django//lib/python/django/middleware/cache.pyt   process_response[   s"    !(   t   __name__t
   __module__t   __doc__R   R   R!   R-   (    (    (    s0   ../Django//lib/python/django/middleware/cache.pyR   8   s
   			t   FetchFromCacheMiddlewarec           B   s    e  Z d  Z d „  Z d „  Z RS(   s)  
    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   sO   t  j |  _ t  j |  _ t t  d t ƒ |  _ t  j |  _	 t
 |  j	 ƒ |  _ d  S(   NR   (   R    R	   R
   R   R   R   R   R   R   R   R   R   (   R   (    (    s0   ../Django//lib/python/django/middleware/cache.pyR   ~   s
    c         C   sß   | j  d k r t | _ d St | |  j d d |  j ƒ} | d k rS t | _ d S|  j j | d ƒ } | d k r¹ | j  d k r¹ t | |  j d d |  j ƒ} |  j j | d ƒ } n  | d k rÒ t | _ d St | _ | S(   sp   
        Checks whether the page is already cached and returns the cached
        version if available.
        t   GETt   HEADR   (   s   GETR3   N(	   t   methodR   R   R*   R   R   R   R   t   get(   R   R   R%   R    (    (    s0   ../Django//lib/python/django/middleware/cache.pyt   process_request…   s     				(   R.   R/   R0   R   R6   (    (    (    s0   ../Django//lib/python/django/middleware/cache.pyR1   v   s   	t   CacheMiddlewarec           B   s   e  Z d  Z d d d „ Z RS(   sÌ   
    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.
    c         K   sL  i  } y9 | d |  _  |  j  d  k	 r5 |  j  | d <n	 d |  _  Wn* t k
 rk t j |  _  |  j  | d <n XyE | d |  _ |  j d  k r— t |  _ n  | d  k	 r° | | d <n  WnC t k
 rö t j |  _ | d  k ré t j | d <q÷ | | d <n X| d  k rt	 t d t
 ƒ |  _ n	 | |  _ t |  j |  |  _ |  j j |  _ d  S(   NR   t
   KEY_PREFIXt    R   t   TIMEOUTR   (   R   R*   t   KeyErrorR    R   R   R   R   R	   R   R   R   R   R   t   default_timeoutR
   (   R   R
   R   t   kwargst   cache_kwargs(    (    s0   ../Django//lib/python/django/middleware/cache.pyR   ¨   s2    	N(   R.   R/   R0   R*   R   (    (    (    s0   ../Django//lib/python/django/middleware/cache.pyR7   ¡   s   N(   R0   t   django.confR    t   django.core.cacheR   R   t   django.utils.cacheR   R   R   R   t   objectR   R1   R7   (    (    (    s0   ../Django//lib/python/django/middleware/cache.pyt   <module>1   s   ">+