ó
Ñ7ec           @   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 d d l 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 = [
        '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.

iÿÿÿÿ(   t   settings(   t   DEFAULT_CACHE_ALIASt   caches(   t   get_cache_keyt   get_max_aget   has_vary_headert   learn_cache_keyt   patch_response_headers(   t   MiddlewareMixint   UpdateCacheMiddlewarec           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s6  
    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         C   sA   t  j |  _ t  j |  _ t  j |  _ t |  j |  _ | |  _	 d  S(   N(
   R    t   CACHE_MIDDLEWARE_SECONDSt   cache_timeoutt   CACHE_MIDDLEWARE_KEY_PREFIXt
   key_prefixt   CACHE_MIDDLEWARE_ALIASt   cache_aliasR   t   cachet   get_response(   t   selfR   (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   __init__@   s
    c         C   s   t  | d ƒ o | j S(   Nt   _cache_update_cache(   t   hasattrR   (   R   t   requestt   response(    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   _should_update_cacheG   s    c            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%| j d k 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È   i0  t   Cookiei    R   t   renderc            s   ˆ j  j ˆ  |  ˆ ƒ S(   N(   R   t   set(   t   r(   t	   cache_keyR   t   timeout(    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   <lambda>f   t    (   iÈ   i0  N(   R   t	   streamingt   status_codet   COOKIESt   cookiesR   R   t   NoneR   R   R   R   R   R   t   callableR   t   add_post_render_callbackR   (   R   R   R   (    (   R   R   R   sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   process_responseJ   s&    "!N(   t   __name__t
   __module__t   __doc__R%   R   R   R(   (    (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyR	   7   s   	t   FetchFromCacheMiddlewarec           B   s#   e  Z d  Z d 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
    so that it'll get called last during the request phase.
    c         C   s5   t  j |  _ t  j |  _ t |  j |  _ | |  _ d  S(   N(   R    R   R   R   R   R   R   R   (   R   R   (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyR   u   s    c         C   sÙ   | j  d k r t | _ d St | |  j d d |  j ƒ} | d k rS t | _ d S|  j j | ƒ } | d k r³ | j  d k r³ t | |  j d d |  j ƒ} |  j j | ƒ } 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   (   R-   R.   N(	   t   methodt   FalseR   R%   R   R   R   t   Truet   get(   R   R   R   R   (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   process_request{   s     				N(   R)   R*   R+   R%   R   R3   (    (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyR,   m   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   sÐ   | |  _  y# | d } | d  k r+ d } n  Wn t k
 rH t j } n X| |  _ y# | d } | d  k rt t } n  Wn t k
 r‘ t j } n X| |  _ | d  k r³ t j	 } n  | |  _
 t |  j |  _ d  S(   NR   R    R   (   R   R%   t   KeyErrorR    R   R   R   R   R   R
   R   R   R   (   R   R   R   t   kwargsR   R   (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyR   Ÿ   s&    	
	
		N(   R)   R*   R+   R%   R   (    (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyR4   ˜   s   N(   R+   t   django.confR    t   django.core.cacheR   R   t   django.utils.cacheR   R   R   R   R   t   django.utils.deprecationR   R	   R,   R4   (    (    (    sA   /usr/local/lib/python2.7/dist-packages/django/middleware/cache.pyt   <module>,   s   (6+