预缓存django REST视图
我使用django实现了REST-ful服务,对于访问的每个ressource,我都希望缓存可能访问的相关数据 例如,ressource预缓存django REST视图,django,web-services,rest,django-views,django-cache,Django,Web Services,Rest,Django Views,Django Cache,我使用django实现了REST-ful服务,对于访问的每个ressource,我都希望缓存可能访问的相关数据 例如,ressourcehttp://www.example.com/publication/1280将给出xml响应: <publication xmlns="http://www.example.com" xmlns:atom="http://www.w3.org/2005/atom"> <abstract>None</abstract> <
http://www.example.com/publication/1280
将给出xml响应:
<publication xmlns="http://www.example.com" xmlns:atom="http://www.w3.org/2005/atom">
<abstract>None</abstract>
<owner>
<atom:link rel="owner" type="application/xml" href="http://www.example.com/user/1">ckpuser</atom:link>
</owner>
<authors>
<author>
<atom:link rel="author" type="application/xml" href="http://www.example.com/author/564">P. Almquist</atom:link>
</author>
</authors>
<comments></comments>
<tags></tags>
<keywords></keywords>
<referencematerials></referencematerials>
<peerreviews></peerreviews>
<fields>
<howpublished>RFC 1349 (Proposed Standard)</howpublished>
<url>http://www.ietf.org/rfc/rfc1349.txt</url>
</fields>
</publication>
没有一个
克普瑟
阿尔姆奎斯特
RFC 1349(拟定标准)
http://www.ietf.org/rfc/rfc1349.txt
然后我想预缓存与Resourceshttp://www.example.com/user/1
和http://www.example.com/author/564
由于在web服务中,给出的响应是某种数据结构,我认为缓存整个响应比缓存queryset要好。如果我们缓存queryset,那么每次访问ressource时都会在模板呈现方面浪费时间
这是一个好方法吗?我错过什么了吗
如果这种方法是正确的,那么如何使用django提供的中间件预缓存视图呢
'django.middleware.cache.UpdateCachedWare'
及
'django.middleware.cache.FetchFromCacheMiddleware'
谢谢试试Django的
基本上,它使用URL(和其他一些东西)作为缓存键,实现方式如下:
from django.views.decorators.cache import cache_page
@cache_page(60 * 15) # cache for 15 minutes
def my_view(request):
...
这将缓存视图的XML输出,正如您所怀疑的那样,当缓存有效时,将比仅缓存queryset需要更少的资源来检索
缓存中间件django.middleware.cache.updateCacheMedidleware
和django.middleware.cache.FetchFromCacheMiddleware
将缓存整个站点,这很可能不是您想要的