î
ªÍ Xj  ã               @   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 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 = [
        '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_headers)ÚMiddlewareMixinc               @   s=   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d S)	ÚUpdateCacheMiddlewarea6  
    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.
    Nc             C   sA   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Úget_response)Úselfr   © r   úE/home/ubuntu/projects/ifolica/build/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_cacheG   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>f   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_responseJ   s&    "!z&UpdateCacheMiddleware.process_response)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r   r   r   r   r   7   s   r   c               @   s1   e  Z d  Z d Z d 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
    so that it'll get called last during the request phase.
    Nc             C   s5   t  j |  _ t  j |  _ t |  j |  _ | |  _ d  S)N)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   u   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_request{   s     				z(FetchFromCacheMiddleware.process_request)r)   r*   r+   r,   r   r2   r   r   r   r   r-   m   s   r-   c               @   s(   e  Z d  Z d Z d 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
 rI t j } Yn X| |  _ y# | d } | d  k ru t } n  Wn t k
 r“ t j } Yn X| |  _ | d  k rµ t j } n  | |  _	 t
 |  j |  _ d  S)Nr   Ú r   )r   ÚKeyErrorr   r   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   r3   ˜   s   r3   N)r,   Zdjango.confr   Zdjango.core.cacher   r   Zdjango.utils.cacher   r   r   r   r	   Zdjango.utils.deprecationr
   r   r-   r3   r   r   r   r   Ú<module>,   s   (6+