def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) response_triple = self.cache.get(key) if not response_triple: # render response to create and cache the content byte string response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() if not response.status_code >= 400 or self.cache_errors: response_triple = (response.rendered_content, response.status_code, response._headers.copy()) self.cache.set(key, response_triple, self.timeout) else: # build smaller Django HttpResponse content, status, headers = response_triple response = HttpResponse(content=content, status=status) response._headers = headers if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) response = self.cache.get(key) if not response: response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render( ) # should be rendered, before picklining while storing to cache if not response.status_code >= 400 or self.cache_errors: response_dict = (response.rendered_content, response.status_code, response._headers) self.cache.set(key, response_dict, self.timeout) else: content, status, headers = response response = HttpResponse(content=content, status=status) response._headers = headers if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key( view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs ) response = self.cache.get(key) if not response: response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() # should be rendered, before picklining while storing to cache if not response.status_code >= 400 or self.cache_errors: response_dict = ( response.rendered_content, response.status_code, response._headers ) self.cache.set(key, response_dict, self.timeout) else: content, status, headers = response response = HttpResponse(content=content, status=status) response._headers = headers if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): flag_name = f'compressed_cache.{view_instance.__class__.__name__}.{view_method.__name__}' flag = get_waffle_flag_model().get(flag_name) # If the flag isn't stored in the database yet, then use the cache # If it is in the database, use the waffle rules for activity # This logic allows us to opt particular pages out of the cache without having # to define all of the flags ahead of time. use_page_cache = (not flag.pk) or flag.is_active(request) if use_page_cache: key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) response_triple = self.cache.get(key) else: logger.info("Skipping page caching for %s", flag_name) response_triple = None if not response_triple: response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() if (not (response.status_code >= 400 or self.cache_errors) and isinstance(response.accepted_renderer, JSONRenderer) and use_page_cache): # Put the response in the cache only if there are no cache errors, response errors, # and the format is json. We avoid caching for the BrowsableAPIRenderer so that users don't see # different usernames that are cached from the BrowsableAPIRenderer html response_triple = ( zlib.compress(response.rendered_content), response.status_code, response._headers.copy(), # pylint: disable=protected-access ) self.cache.set(key, response_triple, self.timeout) else: # If we get data from the cache, we reassemble the data to build a response # We reassemble the pieces from the cache because we can't actually set rendered_content # which is the part of the response that we compress compressed_content, status, headers = response_triple try: decompressed_content = zlib.decompress(compressed_content) except (TypeError, zlib.error): # If we get a type error or a zlib error, the response content was never compressed decompressed_content = compressed_content response = HttpResponse(content=decompressed_content, status=status) response._headers = headers # pylint: disable=protected-access if not hasattr(response, '_closable_objects'): response._closable_objects = [] # pylint: disable=protected-access return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) response_triple = self.cache.get(key) if not response_triple: response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() if (not (response.status_code >= 400 or self.cache_errors) and isinstance(response.accepted_renderer, JSONRenderer)): # Put the response in the cache only if there are no cache errors, response errors, # and the format is json. We avoid caching for the BrowsableAPIRenderer so that users don't see # different usernames that are cached from the BrowsableAPIRenderer html response_triple = ( zlib.compress(response.rendered_content), response.status_code, response._headers.copy(), # pylint: disable=protected-access ) self.cache.set(key, response_triple, self.timeout) else: # If we get data from the cache, we reassemble the data to build a response # We reassemble the pieces from the cache because we can't actually set rendered_content # which is the part of the response that we compress compressed_content, status, headers = response_triple try: decompressed_content = zlib.decompress(compressed_content) except (TypeError, zlib.error): # If we get a type error or a zlib error, the response content was never compressed decompressed_content = compressed_content response = HttpResponse(content=decompressed_content, status=status) response._headers = headers # pylint: disable=protected-access if not hasattr(response, '_closable_objects'): response._closable_objects = [] # pylint: disable=protected-access return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key( view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs ) timeout = self.calculate_timeout(view_instance=view_instance) response_triple = self.cache.get(key) if not response_triple: # render response to create and cache the content byte string response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() if not response.status_code >= 400 or self.cache_errors: response_triple = ( response.rendered_content, response.status_code, response._headers.copy() ) self.cache.set(key, response_triple, timeout) else: # build smaller Django HttpResponse content, status, headers = response_triple response = HttpResponse(content=content, status=status) response._headers = headers if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) timeout = self.calculate_timeout(view_instance=view_instance) response_triple = self.cache.get(key) if not response_triple: # render response to create and cache the content byte string response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render() if not response.status_code >= 400 or self.cache_errors: # django 3.0 has not .items() method, django 3.2 has not ._headers if hasattr(response, '_headers'): headers = response._headers.copy() else: headers = {k: (k, v) for k, v in response.items()} response_triple = (response.rendered_content, response.status_code, headers) self.cache.set(key, response_triple, timeout) else: # build smaller Django HttpResponse content, status, headers = response_triple response = HttpResponse(content=content, status=status) for k, v in headers.values(): response[k] = v if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response