可以缓存Wagtail API的结果还是设置`Cache-Control`标头?

问题描述 投票:0回答:1

我正在尝试从Wagtail缓存一个API视图。我已经搜索了缓存Django REST的方法,并通过在响应中注入Cache-Control找到了一种方法。如下所示:http://www.django-rest-framework.org/api-guide/responses/#standard-httpresponse-attributes

虽然序列化程序是Wagtail的一部分,但此方法不适用于Wagtail。有没有办法可以做到这一点?

django-rest-framework wagtail
1个回答
3
投票

Wagtail API端点在wagtail.api.v2.endpoints(以及其他位置,如wagtail.images.api.v2.endpoints)中定义,可以进行子类化以提供自定义行为,例如在响应上设置其他标头。例如,要将Cache-Control标头添加到PagesAPIEndpoint的详细视图中:

from wagtail.api.v2.endpoints import PagesAPIEndpoint

class CachedPagesAPIEndpoint(PagesAPIEndpoint):
    def detail_view(self, request, pk):
        response = super().detail_view(request, pk)
        response['Cache-Control'] = 'no-cache'
        return response

然后,in your api.py,注册您的自定义CachedPagesAPIEndpoint代替标准PagesAPIEndpoint

© www.soinside.com 2019 - 2024. All rights reserved.