我有一個rails 4應用程序,並試圖實現緩存。我使用@profiles_sidebar.first
緩存鍵來檢查是否創建了新用戶。我不確定這是否正常,因爲仍然有一個數據庫查詢。這是檢查緩存是否需要過期的首選機制?我做得好嗎?緩存後,rails4仍然運行
<% cache(@profiles_sidebar.first) do %>
<% @profiles_sidebar.each do |profile| %>
<%= link_to user_path(profile.user) do %>
<%= truncate(profile.full_name, length: 25) %>
<%= truncate(profile.company, length:25) %>
<% end %>
<% end %>
<% end %>
讀取緩存時
控制檯代碼:
13:31:53 puma.1 | Profile Load (2.2ms) SELECT "profiles".* FROM "profiles" ORDER BY "profiles"."created_at" DESC LIMIT 1
13:31:53 puma.1 | User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (67)
13:31:53 puma.1 | Cache digest for app/views/users/_user_sidebar.html.erb: bfc9447057c94bcfe13c18e391127f2d
13:31:53 puma.1 | Read fragment views/profiles/62-20160331112332689423000/bfc9447057c94bcfe13c18e391127f2d (0.2ms)
13:31:53 puma.1 | Rendered users/_user_sidebar.html.erb (11.8ms)
最大,謝謝。我還會再提出一個問題,因爲我對某些東西還不確定。 –
這是關於命名緩存鍵的新命令:http://stackoverflow.com/questions/36336322/rails4-caching-naming-conventions –