o 3a# @s~dZddlmZddlmZmZddlmZmZm Z m Z m Z ddl m Z Gddde ZGdd d e ZGd d d eeZd 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_ALIAScaches) get_cache_key get_max_agehas_vary_headerlearn_cache_keypatch_response_headers)MiddlewareMixincs2eZdZdZd fdd ZddZddZZS) 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. Ncs:t|tj|_d|_tj|_tj|_ t |j |_ dSN) super__init__rCACHE_MIDDLEWARE_SECONDS cache_timeout page_timeoutCACHE_MIDDLEWARE_KEY_PREFIX key_prefixCACHE_MIDDLEWARE_ALIAS cache_aliasrcacheself get_response __class__9/usr/lib/python3/dist-packages/django/middleware/cache.pyrBs zUpdateCacheMiddleware.__init__cCst|do|jS)N_cache_update_cache)hasattrrrrequestresponserrr_should_update_cacheJsz*UpdateCacheMiddleware._should_update_cachecs||s|S|js|jdvr|S|js|jrt|dr|Sd|ddvr)|SjdurBt|dur<j ndkrB|St |rx|jdkrxt ||j j d t|d rpt|jrp|fd d |Sj ||S) zSet the cache, if needed.)i0Cookieprivatez Cache-ControlrNrr$rrendercsj|Sr )rset)r cache_keyrtimeoutrrpsz8UpdateCacheMiddleware.process_response..)r# streaming status_codeCOOKIEScookiesrgetrrrr rrrrcallabler(add_post_render_callbackr)r rr+rprocess_responseMs2  z&UpdateCacheMiddleware.process_responser )__name__ __module__ __qualname____doc__rr#r6 __classcell__rrrrr 7s  r cs*eZdZdZdfdd ZddZZS)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. Ncs,t|tj|_tj|_t|j|_dSr ) r rrrrrrrrrrrrrs z!FetchFromCacheMiddleware.__init__cCs|jdvr d|_dSt||jd|jd}|durd|_dS|j|}|dur<|jdkr)methodrrrrr3)rr!r,r"rrrprocess_requests   z(FetchFromCacheMiddleware.process_requestr )r7r8r9r:rr@r;rrrrr<ws r<cs"eZdZdZdfdd ZZS)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 st|z|d}|durd}||_Wn tyYnwz|d}|dur*t}||_t|j|_Wn ty=Ynw|durE||_||_ dS)Nrr) r rrKeyErrorrrrrrr)rrrrkwargsrrrrrrs*     zCacheMiddleware.__init__)NNN)r7r8r9r:rr;rrrrrAsrAN)r: django.confrdjango.core.cacherrdjango.utils.cacherrrrr django.utils.deprecationr r r<rArrrrs - @-