ó
TR¹Nc           @   s‘   d  Z  d d l m Z d d l 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 parameter-less 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 a response
  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   cache(   t   get_cache_keyt   learn_cache_keyt   patch_response_headerst   get_max_aget   UpdateCacheMiddlewarec           B   s    e  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   s1   t  j |  _ t  j |  _ t t  d t ƒ |  _ d  S(   Nt   CACHE_MIDDLEWARE_ANONYMOUS_ONLY(   R    t   CACHE_MIDDLEWARE_SECONDSt   cache_timeoutt   CACHE_MIDDLEWARE_KEY_PREFIXt
   key_prefixt   getattrt   Falset   cache_anonymous_only(   t   self(    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyt   __init__@   s    c         C   s½   t  | d ƒ s | j r | S| j d k r1 | S| j d k sD | St | ƒ } | d k rh |  j } n | d k rx | St | | ƒ | r¹ t | | | |  j	 ƒ } t
 j | | | ƒ n  | S(   s   Sets the cache, if needed.t   _cache_update_cachet   GETiÈ   i    N(   t   hasattrR   t   methodt   status_codeR   t   NoneR	   R   R   R   R   t   set(   R   t   requestt   responset   timeoutt	   cache_key(    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyt   process_responseE   s     (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyR   7   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   s1   t  j |  _ t  j |  _ t t  d t ƒ |  _ d  S(   NR   (   R    R   R	   R
   R   R   R   R   (   R   (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyR   i   s    c         C   sÑ   |  j  r$ t | d ƒ s$ t d ‚ n  | j d k s< | j rI t | _ d S|  j  rn | j j	 ƒ  rn t | _ d St
 | |  j ƒ } | d k r™ t | _ d St j | d ƒ } | d k rÄ t | _ d St | _ | S(   sp   
        Checks whether the page is already cached and returns the cached
        version if available.
        t   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.R   t   HEAD(   s   GETs   HEADN(   R   R   t   AssertionErrorR   R   R   R   R   R!   t   is_authenticatedR   R   t   TrueR   t   get(   R   R   R   R   (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyt   process_requestn   s$    						(   R   R   R   R   R'   (    (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyR    a   s   	t   CacheMiddlewarec           B   s    e  Z d  Z d 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         C   sy   | |  _  | d  k r$ t j |  _  n  | |  _ | d  k rH t j |  _ n  | d  k rl t t d t ƒ |  _ n	 | |  _ d  S(   NR   (	   R	   R   R    R   R   R
   R   R   R   (   R   R	   R   R   (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyR   ’   s    		N(   R   R   R   R   R   (    (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyR(   ‹   s   N(   R   t   django.confR    t   django.core.cacheR   t   django.utils.cacheR   R   R   R   t   objectR   R    R(   (    (    (    sD   /home/panlixing/Python_Projects/gaeseries/django/middleware/cache.pyt   <module>1   s   "**