我正在使用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