zoukankan      html  css  js  c++  java
  • Rails中的缓存

    最近学习Rails。

    看到如下代码:

    复制代码
    <% 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 %>不是很明白。研究后有如下发现:

    1、配置

      配置 : /config/environments/development.rb

      config.action_controller.perform_caching = true #开发环境下启动缓存

    2、定义

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

      Module : ActionView::Helpers::CacheHelper

      Type: Instance Public methods

    3、应用

         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 %>
  • 相关阅读:
    bash命令
    集群解析
    临时配置网络(ip,网关,dns)+永久配置
    nginx反向代理三台web
    源码安装nginx
    nginx反向代理+三台web+nfs共享存储实现集群配置
    jQuery学习笔记(一):入门【转】
    你是怎么把字符串“2016-11-16” 变为 “16/11/2016” 的? 【转】
    用css画出三角形【转】
    JS中常遇到的浏览器兼容问题和解决方法【转】
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/4711695.html
Copyright © 2011-2022 走看看