Django 只缓存没有 GET 或 POST 参数的页面?
我正在阅读 django 文档 http://docs.djangoproject.com /en/dev/topics/cache/?from=olddocs 我读到了以下行:
缓存中间件会缓存每个没有 GET 或 POST 参数的页面。
这是否意味着它不会缓存具有 GET 或 POST 参数的页面?如果这是真的,那么它看起来相当愚蠢,因为网站的很大一部分都有一些 GET 或 POST 参数。例如,分页是非常常见的。谁能澄清这一点吗?
谢谢你!
I was reading the django docs at http://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs and I read the following line:
the cache middleware caches every page that doesn't have GET or POST parameters.
Does this mean that it won't cache pages that does have GET or POST parameters? If that's true, then it seems rather silly because a good portion of a website has some GET or POST parameters. Pagination, for example, is extremely common. Can anyone clarify this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完整的 url 用于缓存键。如果我们看一下 django/utils/cache.py 有 <一个href="https://github.com/django/django/blob/979f61abd322507aafced9627702362e541ec34e/django/utils/cache.py#L345" rel="nofollow noreferrer">
get_cache_key
其中转接电话_generate_cache_header_key
。这会获取绝对 URL 的 MD5 哈希值,如活动语言所示。The full url is used for cache keys. If we look at django/utils/cache.py there is
get_cache_key
which in turn calls_generate_cache_header_key
. This gets the MD5 hash of the absolute url as shown with the active language too.通常,如果您的应用程序设计正确,那么使用 GET 或 POST 缓存页面是没有意义的。
使用 POST
POST 数据的页面通常是用户与表单交互的结果。这意味着,缓存 POST 数据可能会缓存删除用户的请求,或者向数据库添加新记录的请求。这可不太好。
使用 GET 的页面
至于 GET 参数,它们旨在用于搜索页面,例如:
缓存这样的页面没有多大意义 - 它们在生命周期中可能只呈现一次。
错误的方式
但是,如果您以错误的方式使用 GET,您将会遇到问题:
视图的参数应作为 url 的一部分传递:
Usually, if your application is designed right, there is no sense of caching pages with GET or POST.
Pages that use POST
POST data is usually a result with user interacting with forms. This means, that caching POST data might cache a request to delete a user, for example, or to add new record to a database. This wouldn't be good.
Pages that use GET
As for the GET parameters, they are meant to use for search pages, like those:
There is no much sense of caching pages like that - they might be rendered only once in the lifetime.
Wrong way
However, you will get problems if you are using GET the wrong way:
Parameters for views should be passed as part of url: