Ruby on rails Rails部分缓存显示视图更改,尽管没有模型更改
遵循“使用Rails 4进行敏捷Web开发”的指南 它涉及产品目录的缓存,以仅重新呈现已更改的产品 我编辑了(config/environments/development.rb): 添加了返回最新更新产品的代码: (app/models/product.rb) 最后更新了缓存的存储索引: (app/views/store/index.html.erb)Ruby on rails Rails部分缓存显示视图更改,尽管没有模型更改,ruby-on-rails,ruby-on-rails-4,caching,Ruby On Rails,Ruby On Rails 4,Caching,遵循“使用Rails 4进行敏捷Web开发”的指南 它涉及产品目录的缓存,以仅重新呈现已更改的产品 我编辑了(config/environments/development.rb): 添加了返回最新更新产品的代码: (app/models/product.rb) 最后更新了缓存的存储索引: (app/views/store/index.html.erb) 你的实用目录 这本书指出,测试缓存是否工作的唯一方法是更改视图,因此我所做的只是在一个或两个缓存标记中添加一些lorem ipsum,但浏览
你的实用目录
这本书指出,测试缓存是否工作的唯一方法是更改视图,因此我所做的只是在一个或两个缓存标记中添加一些lorem ipsum,但浏览器在刷新时立即显示更改。。。令人沮丧的!有什么线索吗?尝试使用
<% cache ['v1', Product.order(updated_at: :desc).last] do %>
而不是
<% cache ['store', Product.latest] do %>
集合的缓存密钥未更改。这是因为
cache['store',Product.latest]
并不像您预期的那么聪明。如果查看日志,您将看到所使用的缓存键是一个文本,其中包含类似ActiveRecord::Relation::Products的内容
使用上次更新的\u at值缓存['store',Product.latest.max(:updated\u at)]
以获得更好的结果
在Rails 5中,由于几周前的原因,这将变得更容易。您在
产品上遇到了一个bug。最新的,它正在调用自己。您可能有一个Product.order(:updated_at)。最后(10)
如果没有出现错误,可能是明显的,但您在更改环境文件后重新启动了服务器吗?
<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 %>
<% cache ['v1', Product.order(updated_at: :desc).last] do %>
<% cache ['store', Product.latest] do %>