使用继承资源进行缓存

发布于 2024-11-06 18:37:22 字数 180 浏览 4 评论 0原文

我已经使用继承资源插件构建了一个应用程序,并准备投入生产。

现在我正在研究为同一应用程序实现缓存的方法。由于我有一些依赖于 current_user 的页面片段,我将使用片段缓存机制。

有人可以向我提供一个指针或资源吗?它有助于为基于继承资源的视图和控制器实现片段缓存?

谢谢, 阿贾伊·库马尔 G

I have build an application using inherited_resource plugin and its ready to go into production.

Now I am looking at ways to implement caching for the same application. As I have some fragments of the page which are dependent on the current_user I am going to use the fragment caching mechanism.

Can someone please provide me with the a pointer or resource which helps in implementing fragment caching for inherited_resource based views and controllers?

Thanks,
Ajay Kumar G

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2024-11-13 18:37:22

我不使用inherited_resources,但我的理解是它只是控制器的快捷方式。您的视图应该是标准的,在这种情况下,您可以使用片段缓存,如 Rails 指南中所述 http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching

你尝试过吗?如果这不起作用,请向我们展示您的观点。


编辑:对评论的回复。

好吧,所以一般来说,如果您使用片段缓存,那么不调用控制器操作是没有意义的,因为您不会设置任何变量,如果没有变量,那么为什么不使用页面缓存呢?

为了防止查询触发,带有 arel 的 ActiveRecord 3.0 是你的朋友。例如,如果您将 @posts = Post.where(:published => true) 放入操作中,那么直到您在视图中调用 @posts.each 之前,它不会被调用,因此,如果您将其放入缓存块中,它就会被调用。永远不会被调用。我不确定继承资源默认做什么,但考虑到它是由 José Valim 编写的,我想他默认会使用 ActiveRecord 的该功能,如果不是的话,很容易自定义。您可以通过打开开发环境的缓存并观察日志以了解正在触发哪些查询来对其进行测试。

I don't use inherited_resources, but my understanding is that it's just a shortcut for controllers. Your views should just be standard, in which case you can use fragment caching as described in the Rails Guide at http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching

Have you tried that? If that doesn't work show us what your views look like.


edit: response to the comment.

Ah okay, so in general if you are using fragment caching it doesn't make sense not to call the controller action, because you won't have any variables set, and if there are no variables then why are you not using page caching?

In order to prevent queries from firing, ActiveRecord 3.0 with arel is your friend. For instance, if you put @posts = Post.where(:published => true) in your action then it will not be called until you call @posts.each in the view, so if you put that inside the cache block it will never be called. I'm not sure what inherited_resources does by default, but given that it's written by José Valim I imagine he would be using that feature of ActiveRecord by default, if not it is easy to customize. You can test it out by turning on caching for your development environment and watching the logs to see what queries are being fired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文