如何测试django缓存?
有没有办法确定页面来自生产服务器和开发服务器上的缓存?
该解决方案不应该涉及缓存中间件,因为并非每个项目都使用它们。 尽管解决方案本身可能是一个中间件。
在我看来,仅仅检查数据是否过时并不是一种非常安全的测试方法。
Is there a way to be sure that a page is coming from cache on a production server and on the development server as well?
The solution shouldn't involve caching middleware because not every project uses them. Though the solution itself might be a middleware.
Just checking if the data is stale is not a very safe testing method IMO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们进行了大量的组件缓存,但并非所有组件都会同时更新。 因此,我们在普遍包含的上下文处理器中设置主机和时间戳值。 在每个模板片段的顶部,我们插入:
component_name 只是使查看源代码和搜索该字符串变得容易。
我们所有作为对象详细信息页面的视图都定义了一个上下文变量“page_object”,我们将其置于 base.html 模板 master 的顶部:
class_id() 是我们所有主要内容类使用的超类中的方法。 这只是:
如果您加载页面并且任何时间戳都超过几秒,则很有可能该组件已被缓存。
We do a lot of component caching and not all of them are updated at the same time. So we set host and timestamp values in a universally included context processor. At the top of each template fragment we stick in:
The component_name just makes it easy to do a View Source and search for that string.
All of our views that are object-detail pages define a context variable "page_object" and we have this at the top of the base.html template master:
class_id() is a method from a super class used by all of our primary content classes. It is just:
If you load a page and any of the timestamps are more than few seconds old, it's a pretty good bet that the component was cached.
Peter Rowells 的建议效果很好,但你不需要自定义模板上下文处理器
对于时间戳。 您可以简单地使用模板标签:
Peter Rowells suggestion works well, but you don't need a custom template context processor
for timestamps. You can simply use the template tag:
模拟视图,点击页面,然后查看模拟是否被调用。 如果不是,则使用缓存。
Mock the view, hit the page, and see if the mock was called. if it was not, the cache was used instead.
使用缓存的原因是为了提高性能。 通过对服务器运行负载测试来测试性能。 如果服务器的性能符合您的需求,那么一切就都准备好了!
The reason you use caches is to improve performance. Test the performance by running a load test against your server. If the server's performance matches your needs, then you are all set!