Ñò
h<nMc           @   s—   d  Z  d d k l Z d d k l Z l Z d d k l Z l Z l	 Z	 l
 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  i |  _ t  i |  _ t t  d t ƒ |  _ t  i |  _	 t
 |  i	 ƒ |  _ 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(    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   __init__A   s
    c         C   s)   y | i  i SWn t j
 o t SXd  S(   N(   t   sessiont   accessedt   AttributeErrorR   (   R   t   request(    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   _session_accessedH   s    c         C   sr   t  | d ƒ p | i o t S|  i oC |  i | ƒ o3 t  | d ƒ p
 t d ‚ | i i ƒ  o t Sn 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(    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   _should_update_cacheN   s    	c            sø   ˆ  i  | | ƒ p | S| i d j p | St | ƒ ‰ ˆ d j o ˆ  i ‰ n ˆ d j o | St | ˆ ƒ ˆ o{ t | | ˆ ˆ  i d ˆ  i ƒ‰ t	 | d ƒ o0 t
 | i ƒ o  | i ‡ ‡  ‡ f d †  ƒ qô ˆ  i i ˆ | ˆ ƒ n | S(   s   Sets the cache, if needed.iÈ   i    R   t   renderc            s   ˆ i  i ˆ  |  ˆ ƒ S(    (   R   t   set(   t   r(   t	   cache_keyR   t   timeout(    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   <lambda>p   s    N(   R!   t   status_codeR   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.6/dist-packages/django/middleware/cache.pyt   process_response[   s"    ! (   t   __name__t
   __module__t   __doc__R   R   R!   R,   (    (    (    sA   /usr/local/lib/python2.6/dist-packages/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  i |  _ t  i |  _ t t  d t ƒ |  _ t  i |  _	 t
 |  i	 ƒ |  _ d  S(   NR   (   R    R	   R
   R   R   R   R   R   R   R   R   R   (   R   (    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyR   ~   s
    c         C   sè   | i  d j o t | _ d St | |  i d d |  i ƒ} | d j o t | _ d S|  i i | d ƒ } | d j oG | i  d j o7 t | |  i d d |  i ƒ} |  i i | d ƒ } n | d j o t | _ d St | _ | S(   sp   
        Checks whether the page is already cached and returns the cached
        version if available.
        t   GETt   HEADR   (   R1   R2   N(	   t   methodR   R   R)   R   R   R   R   t   get(   R   R   R%   R    (    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   process_request…   s     				(   R-   R.   R/   R   R5   (    (    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyR0   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   sZ  h  } y; | d |  _  |  i  d  j	 o |  i  | d <n
 d |  _  Wn, t j
 o  t i |  _  |  i  | d <n XyI | d |  _ |  i d  j o t |  _ n | d  j	 o | | d <n WnG t j
 o; t i |  _ | d  j o t i | d <q| | d <n X| d  j o t	 t d t
 ƒ |  _ n
 | |  _ t |  i |  |  _ |  i i |  _ 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(    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyR   ¨   s2    	N(   R-   R.   R/   R)   R   (    (    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyR6   ¡   s   N(   R/   t   django.confR    t   django.core.cacheR   R   t   django.utils.cacheR   R   R   R   t   objectR   R0   R6   (    (    (    sA   /usr/local/lib/python2.6/dist-packages/django/middleware/cache.pyt   <module>1   s   ">+