Rails中的緩存

最近學習Rails。緩存

看到以下代碼:ruby

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>

<h1>Your Pragmatic Catalog</h1>

<!-- START_HIGHLIGHT -->
<% cache ['store', Product.latest] do %>
<!-- END_HIGHLIGHT -->
  <% @products.each do |product| %>
<!-- START_HIGHLIGHT -->
    <% cache ['entry', product] do %>
<!-- END_HIGHLIGHT -->
      <div class="entry">
        <%= image_tag(product.image_url) %>
        <h3><%= product.title %></h3>
        <%= sanitize(product.description) %>
        <div class="price_line">
    <!-- START:currency -->
          <span class="price"><%= number_to_currency(product.price) %></span>
    <!-- END:currency -->
        </div>
      </div>
<!-- START_HIGHLIGHT -->
    <% end %>
<!-- END_HIGHLIGHT -->
  <% end %>
<!-- START_HIGHLIGHT -->
<% end %>
<!-- END_HIGHLIGHT -->

對其中<% cache ['store', Product.latest] do %>不是很明白。研究後有以下發現:學習

一、配置this

  配置 : /config/environments/development.rburl

  config.action_controller.perform_caching = true #開發環境下啓動緩存spa

二、定義code

  File : lib/ruby/gems/2.0.0/gems/actionpack-4.0.3/lib/action_view/helpers/cache_helper.rborm

  Module : ActionView::Helpers::CacheHelperblog

  Type: Instance Public methodsip

三、應用

     When using this method, you list the cache dependency as the name of the cache, like so:

    <% cache project do %>
        <b>All the topics on this project</b>
        <%= render project.topics %>
    <% end %>

views/projects/123-20120806214154/7a1156131a6928cb0026877f8b749ac9

   ^class   ^id  ^updated_at    ^template tree digest

     If your template cache depends on multiple sources (try to avoid this to keep things simple),

     you can name all these dependencies as part of an array:

    <% cache [ project, current_user ] do %>
        <b>All the topics on this project</b>
        <%= render project.topics %>
    <% end %>
相關文章
相關標籤/搜索