<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from django . utils . cache import get_cache_key , learn_cache_key , patch_response_headers , get_max_age , has_vary_header <EOL> class UpdateCacheMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cache_timeout = settings . CACHE_MIDDLEWARE_SECONDS <EOL> self . key_prefix = settings . CACHE_MIDDLEWARE_KEY_PREFIX <EOL> self . cache_anonymous_only = getattr ( settings , '<STR_LIT>' , False ) <EOL> def _should_update_cache ( self , request , response ) : <EOL> if not hasattr ( request , '<STR_LIT>' ) or not request . _cache_update_cache : <EOL> return False <EOL> if self . cache_anonymous_only and has_vary_header ( response , '<STR_LIT>' ) : <EOL> assert hasattr ( request , '<STR_LIT:user>' ) , "<STR_LIT>" <EOL> if request . user . is_authenticated ( ) : <EOL> return False <EOL> return True <EOL> def process_response ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> if not self . _should_update_cache ( request , response ) : <EOL> return response <EOL> if request . method != '<STR_LIT:GET>' : <EOL> return response <EOL> if not response . status_code == <NUM_LIT:200> : <EOL> return response <EOL> timeout = get_max_age ( response ) <EOL> if timeout == None : <EOL> timeout = self . cache_timeout <EOL> elif timeout == <NUM_LIT:0> : <EOL> return response <EOL> patch_response_headers ( response , timeout ) <EOL> if timeout : <EOL> cache_key = learn_cache_key ( request , response , timeout , self . key_prefix ) <EOL> cache . set ( cache_key , response , timeout ) <EOL> return response <EOL> class FetchFromCacheMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cache_timeout = settings . CACHE_MIDDLEWARE_SECONDS <EOL> self . key_prefix = settings . CACHE_MIDDLEWARE_KEY_PREFIX <EOL> self . cache_anonymous_only = getattr ( settings , '<STR_LIT>' , False ) <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not request . method in ( '<STR_LIT:GET>' , '<STR_LIT>' ) or request . GET : <EOL> request . _cache_update_cache = False <EOL> return None <EOL> cache_key = get_cache_key ( request , self . key_prefix ) <EOL> if cache_key is None : <EOL> request . _cache_update_cache = True <EOL> return None <EOL> response = cache . get ( cache_key , None ) <EOL> if response is None : <EOL> request . _cache_update_cache = True <EOL> return None <EOL> request . _cache_update_cache = False <EOL> return response <EOL> class CacheMiddleware ( UpdateCacheMiddleware , FetchFromCacheMiddleware ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache_timeout = None , key_prefix = None , cache_anonymous_only = None ) : <EOL> self . cache_timeout = cache_timeout <EOL> if cache_timeout is None : <EOL> self . cache_timeout = settings . CACHE_MIDDLEWARE_SECONDS <EOL> self . key_prefix = key_prefix <EOL> if key_prefix is None : <EOL> self . key_prefix = settings . CACHE_MIDDLEWARE_KEY_PREFIX <EOL> if cache_anonymous_only is None : <EOL> self . cache_anonymous_only = getattr ( settings , '<STR_LIT>' , False ) <EOL> else : <EOL> self . cache_anonymous_only = cache_anonymous_only </s>
