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

时间:2022-10-10 09:23:10

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

假设我有控制器home_controller.rb和动作索引。

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?

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?

页面将在首次未登录用户时缓存。现在,每个登录用户也会看到未登录的内容。有没有办法在不更改网址的情况下分离此操作?

3 个解决方案

#1


1  

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

cache_if和cache_unless现在似乎是正确的方法:

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

Your code:

<% cache_unless @user.changed?, [ @user, 'form' ] do %>

<%cache_unless @ user.changed?,[@ user,'form'] do%>

#2


6  

What you want couldn't be achieved;

你想要的东西无法实现;

A page is cached or is not cached. The process checks the existence of a html file or process it.

页面已缓存或未缓存。该过程检查html文件的存在或处理它。

Still you have two alternatives:

你还有两种选择:

  • use action caching or fragment caching (not recommended)

    使用动作缓存或片段缓存(不推荐)

  • more recommended: load user specific part with ajax so you'll only have one page always cached and specific data inserted dynamically (like *)

    更推荐:使用ajax加载用户特定的部分,这样你只需要一个页面总是被缓存并且动态插入特定的数据(比如*)

Here are state of the art explanations: http://railslab.newrelic.com/scaling-rails

以下是最先进的解释:http://railslab.newrelic.com/scaling-rails

#3


3  

The accepted answer is now out of date, as conditional caching is now available in Rails 4. A pull request was merged into 4.0.0rc1 that allows for :if and :unless parameters to be passed to cache. This allows templates to be DRY, since there is no longer a need to duplicate the conditionally cached block.

接受的答案现已过时,因为条件缓存现在可用于Rails 4.拉取请求已合并到4.0.0rc1中,允许:if和:除非将参数传递给缓存。这允许模板为DRY,因为不再需要复制有条件缓存的块。

Usage:

<% 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 %>

#1


1  

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

cache_if和cache_unless现在似乎是正确的方法:

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

Your code:

<% cache_unless @user.changed?, [ @user, 'form' ] do %>

<%cache_unless @ user.changed?,[@ user,'form'] do%>

#2


6  

What you want couldn't be achieved;

你想要的东西无法实现;

A page is cached or is not cached. The process checks the existence of a html file or process it.

页面已缓存或未缓存。该过程检查html文件的存在或处理它。

Still you have two alternatives:

你还有两种选择:

  • use action caching or fragment caching (not recommended)

    使用动作缓存或片段缓存(不推荐)

  • more recommended: load user specific part with ajax so you'll only have one page always cached and specific data inserted dynamically (like *)

    更推荐:使用ajax加载用户特定的部分,这样你只需要一个页面总是被缓存并且动态插入特定的数据(比如*)

Here are state of the art explanations: http://railslab.newrelic.com/scaling-rails

以下是最先进的解释:http://railslab.newrelic.com/scaling-rails

#3


3  

The accepted answer is now out of date, as conditional caching is now available in Rails 4. A pull request was merged into 4.0.0rc1 that allows for :if and :unless parameters to be passed to cache. This allows templates to be DRY, since there is no longer a need to duplicate the conditionally cached block.

接受的答案现已过时,因为条件缓存现在可用于Rails 4.拉取请求已合并到4.0.0rc1中,允许:if和:除非将参数传递给缓存。这允许模板为DRY,因为不再需要复制有条件缓存的块。

Usage:

<% 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 %>