Eja*_*rim 5 caching ruby-on-rails ruby-on-rails-4
我正在关注一本名为Agile Web Development With Rails 4的书,我在理解cache ['store', Product.latest]视图文件中的功能时遇到了问题.
#static function latest is defined in the model
def self.latest
Product.order(:updated_at).last
end
#here is my view file
<% cache['store',Product.latest] do %>
<% @products.each do|product| %>
<% cache['entry',product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
</div>
</div>
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
助手执行块的内容,并使用给定的键将结果cache(key) { ... }缓存一定的时间。
该文档详细解释了所有各种选项和功能。
在您的情况下,['store',Product.latest]是构建缓存键名称的参数。数组中的项目被连接起来以产生String类似于store/products/100-20140101-163830然后用作缓存键来存储块的结果的结果。
之所以Product.latest作为缓存键的参数传递,是为了确保在将新产品添加到数据库后片段立即过期。这种方法通常称为基于密钥的过期模型。
| 归档时间: |
|
| 查看次数: |
176 次 |
| 最近记录: |