条件页面缓存[解决方案:条件片段缓存]

编程入门 行业动态 更新时间:2024-10-26 10:31:04
本文介绍了条件页面缓存[解决方案:条件片段缓存]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我的控制器 home_controller.rb 的操作为 index 。

Suppose that I have controller home_controller.rb with action index.

我想缓存索引页,所以我在做:

I want to cache index page so I'm doing:

caches_page :index

,但希望它仅对未登录的用户进行缓存。如果我将条件作为:

but want it to cache only for users that are not signed in. If I'll make conditional like:

caches_page :index, :if => :user_not_signed_in?

页面将被缓存,而第一个未登录的用户来了。现在,每个登录用户也可以看到未登录的内容。有没有办法在不更改URL的情况下分隔此操作?

Page will be cached while first not logged in user comes. Now every logged in user also see not-logged in content. Is there a way to separate this action without changing url?

推荐答案

cache_if 和 cache_unless 似乎是现在执行此操作的正确方法:

cache_if and cache_unless seems to be the correct way to do this now:

cache_if(condition, name = {}, options = nil, &block) cache_unless(condition, name = {}, options = nil, &block)

您的代码:

<%cache_unless @ user.changed?,[@用户,表单]做%>

更多推荐

条件页面缓存[解决方案:条件片段缓存]

本文发布于:2023-11-29 01:44:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   条件   片段   解决方案   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!