我正在使用Rails4编写敏捷Web开发书,我刚刚阅读了关于缓存视图各部分以避免压倒数据库的(第一部分)部分。 我当然在开发环境的配置中将缓存选项设置为true。Rails4在敏捷Web开发中使用Rails4
问题是缓存似乎没有工作。 这里是我的应用程序/视图/存储/ index.html.erb文件,酷似书中给出的,启用缓存:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% 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 %>
这里是Rails服务器记录,充分显示出DATABSE在多次访问(虽然行提到缓存):http://pastebin.com/v2jGiHKL
这里是我的应用程序/视图/存储/ index.html.erb文件,其中我尝试别的东西缓存:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache('caching') do %>
<% @products.each do |product| %>
<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 %>
这里是相应的日志,显示出cach ing是成功的(因为数据库没有被查询):http://pastebin.com/ZTk9A9RA
有人可以解释为什么一个似乎工作,而不是其他,或者第一个应该如何工作?谢谢:)
请注意,在本书中,它说,启用缓存后,如果更改是在缓存块内进行的,则重新加载商店页面不应显示store/index.html.erb的新部分;但它确实在两种情况下。任何想法 ?
在您的第一个日志中,只有一个“GET”/“'。你也可以向我们展示控制器吗? –
是不是因为我在第二种情况下只访问过一次页面,在第一种情况下是两次?(在这种情况下,你的意思是相反的,对吧?) 总之,这里是我的控制器: http://pastebin.com/J6dpxe7D 而 http://pastebin.com/aKTUFX82 – GraawrImaTiger