如何为django预缓存模板
How to pre-cache a template for django
我有一个模板,我想为 django 缓存它。 url 是这样的:
/cache_my_page/<object_number>/
这里大约有 100 万个对象 -- 所以我想缓存 100 万个页面。有没有办法在用户加载之前预先缓存所有这些页面?或者 django 不提供那个,我需要深入研究 redis 或 memcache 之类的东西来做到这一点?
您可以在请求-响应之外编译模板,但这可能不是您想要的。 Django 适用于动态内容。尽管如果你的对象几乎从不改变,你当然可以遍历你的对象,将请求的结果内容转储到一个静态文件中,并让它由 nginx 提供服务。
https://docs.djangoproject.com/en/1.7/ref/templates/api/#compiling-a-string
尽管对于动态内容也有更优雅的解决方案。
此处详细描述了如何避免重新呈现之前请求的 url:
http://www.djangobook.com/en/2.0/chapter15.html
或者看看像 Varnish 这样的解决方案将 Django 排除在外:
Varnish is a piece of software that sits between our load balancers
and our Django backends and acts as an HTTP caching layer. What this
means is that it can cache the entire HTTP response without even
hitting a Django server, if we know that request won’t be unique.
http://blog.disqus.com/post/62187806135/scaling-django-to-8-billion-page-views
我有一个模板,我想为 django 缓存它。 url 是这样的:
/cache_my_page/<object_number>/
这里大约有 100 万个对象 -- 所以我想缓存 100 万个页面。有没有办法在用户加载之前预先缓存所有这些页面?或者 django 不提供那个,我需要深入研究 redis 或 memcache 之类的东西来做到这一点?
您可以在请求-响应之外编译模板,但这可能不是您想要的。 Django 适用于动态内容。尽管如果你的对象几乎从不改变,你当然可以遍历你的对象,将请求的结果内容转储到一个静态文件中,并让它由 nginx 提供服务。
https://docs.djangoproject.com/en/1.7/ref/templates/api/#compiling-a-string
尽管对于动态内容也有更优雅的解决方案。 此处详细描述了如何避免重新呈现之前请求的 url:
http://www.djangobook.com/en/2.0/chapter15.html
或者看看像 Varnish 这样的解决方案将 Django 排除在外:
Varnish is a piece of software that sits between our load balancers and our Django backends and acts as an HTTP caching layer. What this means is that it can cache the entire HTTP response without even hitting a Django server, if we know that request won’t be unique.
http://blog.disqus.com/post/62187806135/scaling-django-to-8-billion-page-views