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

问题描述

|| 假设我有控制器“ 0”和动作“ 1”。 我想缓存索引页,所以我在做:
caches_page :index
但只希望它为未登录用户缓存。如果我将条件设置为:
caches_page :index,:if => :user_not_signed_in?
当第一个登录用户到来时,页面将被缓存。现在,每个登录用户还可以看到未登录内容。有没有办法在不更改url的情况下分隔此操作?     

解决方法

        cache_if和cache_unless似乎是现在执行此操作的正确方法:
cache_if(condition,name = {},options = nil,&block)
cache_unless(condition,&block)
您的代码:
<% cache_unless @user.changed?,[ @user,\'form\' ] do %>
    ,        您想要的东西无法实现; 页面已缓存或未缓存。该过程检查html文件的存在或对其进行处理。 仍然有两种选择: 使用动作缓存或片段缓存(不推荐) 更加推荐:用ajax加载用户特定的部分,这样您始终只缓存一页,并且动态插入特定的数据(例如stackoverflow) 以下是最新的解释:http://railslab.newrelic.com/scaling-rails     ,        现在可接受的答案已经过时,因为Rails 4中现在可以使用条件缓存了。拉取请求已合并到4.0.0rc1中,允许将
:if
:unless
参数传递到
cache
。由于不再需要复制条件缓存的块,因此可以使模板成为DRY。 用法:
<% cache [ @user,\'form\' ],:unless => @user.changed? do %>
  <%= render partial: \'shared/error_messages\',locals: {instance: @user} %>
  <%= form_for @user do |f| %>
    <div class=\"field\">
      <div><%= f.label :name %></div>
      <div><%= f.text_field :name %></div>
    </div>
    <div class=\"actions\">
      <%= f.submit %>
    </div>
  <% end %>
<% end %>