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 try: data = content.decode() data = json.loads(data) response = DrfResponse(data=data, status=status) response._headers = headers if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response except: pass 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.generate_key(request) data = self.cache.get(key) if not data: response = view_method(view_instance, request, *args, **kwargs) if not response.status_code >= 400 or self.cache_errors: self.cache.set(key, response.data, self.timeout) else: response = Response(data=data) 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) is_no_cache = False is_no_cache_fun = getattr(view_instance, 'is_no_cache', None) if is_no_cache_fun and is_no_cache_fun(request): is_no_cache = True response = None if not is_no_cache: response = self.cache.get(key) if getattr(settings, "REDIS_ENABLE", False) else None 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: if not is_no_cache: if getattr(settings, "REDIS_ENABLE", False): if isinstance(response, Response): self.cache.set(key, response.data, self.timeout) else: self.cache.set(key, response, self.timeout) handle_refresh_cache_fun = getattr(view_instance, 'handle_refresh_cache', None) if handle_refresh_cache_fun: handle_refresh_cache_fun(request=request, key=key, cache=self.cache) if not isinstance(response, Response): response = Response(data=response) if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response
def process_cache_response(self, view_instance, view_method, request, args, kwargs): body = request.body if type(body) is str: body = body.encode('utf-8') hash = hashlib.md5(body).hexdigest() bulk_hash_meta = request.META.get('HTTP_X_BULK_HASH') if bulk_hash_meta and (hash != bulk_hash_meta): return HttpResponseBadRequest('INVALID X-Bulk-Hash') filter = { 'hash': bulk_hash_meta, 'response_status_code__gte': 200, 'response_status_code__lt': 300 } transaction = GiscubeTransaction.objects.filter(**filter).first() if not transaction: response = None error = None try: response = view_method(view_instance, request, *args, **kwargs) except Exception as e: error = { 'error': str(e), 'traceback': '\n'.join(format_tb(e.__traceback__)) } raise finally: self.log_transaction(request, response=response, error=error) else: response = Response(data=json.loads(transaction.response_body), status=transaction.response_status_code, headers=transaction.response_headers) view_instance.headers = transaction.response_headers response = view_instance.finalize_response(request, response) if not hasattr(response, '_closable_objects'): response._closable_objects = [] return response