Exemple #1
0
def _patch_header(response: HttpResponse, status: Status) -> None:
    # Patch cache-control with no-cache if it is not already set.
    if status == Status.SKIP and not response.get("Cache-Control", None):
        response["Cache-Control"] = CacheControl.NOCACHE.value
    # Add our custom header.
    if wagtailcache_settings.WAGTAIL_CACHE_HEADER:
        response[wagtailcache_settings.WAGTAIL_CACHE_HEADER] = status.value
Exemple #2
0
 def to_djangoresponse(self):
     """django用のレスポンス.
     """
     content_type = self.__headers.get('content-type', 'text/plain')
     self.set_header('content-length', str(len(self.__body)))
     django_response = DjangoHttpResponse(self.__body, content_type=content_type, status=self.status)
     for k, v in self.__headers.items():
         django_response[k] = v
     if self.status == 301:
         django_response = HttpResponsePermanentRedirect(django_response.get('location'))
     elif self.status == 302:
         django_response = HttpResponseRedirect(django_response.get('location'))
     for k, arr in self.__cookies.items():
         v, expires, domain = arr
         django_response.set_cookie(k, v, expires=expires, domain=domain)
     return django_response
Exemple #3
0
        def wrapper(_self, request, **kwargs):
            redis_client = get_redis()

            content, content_type = redis_client.hmget(key, 'content',
                                                       'content_type')
            if content:
                http_response = HttpResponse(content=content,
                                             content_type=content_type)
                return http_response

            # 调用原函数
            response = func(_self, request, **kwargs)
            # FIXME response.data
            http_response = HttpResponse(render.render(response.data),
                                         content_type=render.media_type)
            mapping = {
                'content': http_response.content,
                'content_type': http_response.get('Content-Type'),
            }
            redis_client.hmset(key, mapping)
            # 返回
            return http_response
Exemple #4
0
    def process_response(self, request: WSGIRequest,
                         response: HttpResponse) -> HttpResponse:
        if not wagtailcache_settings.WAGTAIL_CACHE:
            return response

        if getattr(request, "_wagtailcache_skip", False):
            # If we should skip this response, add header and return.
            _patch_header(response, Status.SKIP)
            return response

        if not getattr(request, "_wagtailcache_update", False):
            # We don't need to update the cache, just return.
            return response

        # Check if the response is cacheable
        # Don't cache private or no-cache responses.
        # Do cache 200, 301, 302, 304, and 404 codes so that wagtail doesn't
        #   have to repeatedly look up these URLs in the database.
        # Don't cache streaming responses.
        is_cacheable = (CacheControl.NOCACHE.value not in response.get(
            "Cache-Control", "")
                        and CacheControl.PRIVATE.value not in response.get(
                            "Cache-Control", "")
                        and response.status_code in (200, 301, 302, 304, 404)
                        and not response.streaming)
        # Don't cache 200 responses that set a user-specific cookie in response
        # to a cookie-less request (e.g. CSRF tokens).
        if is_cacheable and response.status_code == 200:
            is_cacheable = not (not request.COOKIES and response.cookies
                                and has_vary_header(response, "Cookie"))

        # Allow the user to override our caching decision.
        for fn in hooks.get_hooks("is_response_cacheable"):
            result = fn(response, is_cacheable)
            if isinstance(result, bool):
                is_cacheable = result

        # If we are not allowed to cache the response, just return.
        if not is_cacheable:
            # Add response header to indicate this was intentionally not cached.
            _patch_header(response, Status.SKIP)
            return response

        # Try to get the timeout from the ``max-age`` section of the
        # ``Cache-Control`` header before reverting to using the cache's
        # default.
        timeout = get_max_age(response)
        if timeout is None:
            timeout = self._wagcache.default_timeout
        patch_response_headers(response, timeout)
        if timeout:
            cache_key = learn_cache_key(request,
                                        response,
                                        timeout,
                                        None,
                                        cache=self._wagcache)
            if isinstance(response, SimpleTemplateResponse):
                response.add_post_render_callback(
                    lambda r: self._wagcache.set(cache_key, r, timeout))
            else:
                self._wagcache.set(cache_key, response, timeout)

            # Add a response header to indicate this was a cache miss.
            _patch_header(response, Status.MISS)

        return response