我有以下查詢:緩存查詢,除非模型更新 - 軌道
def home
@posts = Post.where(:isGroup => "public").order('created_at DESC').page(params[:page]).per(20)
end
我想,除非將新信息添加到Post模型緩存此查詢。
當我使用
expires_in 5.minutes, public: true
它緩存整個動作,而不只是查詢(和攪亂整個頁面,因爲結果是一個無限滾動的開始,並且頁面有一個登錄) 。
當我這樣做:
cache "home_post", expires_in: 10.minutes do
@posts = Post.where(:isGroup => "public").order('created_at DESC').page(params[:page]).per(20)
end
它似乎並沒有表現出任何的性能提升,並在添加新職位是不更新。
什麼是最好的方式來緩存查詢?
謝謝。
如果我這樣做,我會得到一個錯誤(TypeError(無法轉儲匿名類#<模塊:0x007fde45a07c80>):Rails.cache.fetch(「homepage_posts」,:expires_in => 5.minutes)do @ (20) 結束 – user749798
我會以這種方式緩存它'Rails.cache。(=)「public」)。order('created_at DESC')。page(params [:page])。 write(「homepage_posts」,Post.where(:isGroup =>「public」)。order('created_at DESC')。page(params [:page])。per(20).all)'''.all' caches記錄,而不僅僅是活動記錄實例,我會用同樣的方式讀取它'Rails.cache.read('homepage_posts')' – shishirmk