2012-08-11 76 views
0

我想了解爲什麼我的Rails操作緩存不是基於正確的URL請求緩存& params。嘗試使用memcached緩存Rails多頁面視圖

例如,當我請求page/2時,緩存系統返回page

這是日誌奇響應的示例:

Started GET "/clips/page/2" for 127.0.0.1 at 2012-08-11 16:46:42 -0400 
Processing by ClipsController#index as HTML 
    Parameters: {"page"=>"2"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
Cache read: views/localhost:3000/clips/page 
Read fragment views/localhost:3000/clips/page (0.3ms) 
    Rendered text template within layouts/application (0.0ms) 
Completed 200 OK in 50ms (ActiveRecord: 0.2ms) 

我覺得奇怪的是,清楚地被要求的URL是/clips/page/2,而響應讀「片段」 Read fragment views/localhost:3000/clips/page (0.3ms)

在我的控制,我有:

caches_action :index, :layout => false

和一個非常簡單的索引操作:

def index 
    @clips = current_user.clips.page(params[:page]).order('created_at DESC') 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @clips } 
    end 
end 

我development.rb配置文件,在那裏我測試這使得cachine像所以:

# caching 
    config.perform_caching = true # cache test 
    config.action_controller.perform_caching = true # cache test 
    # config.action_controller.perform_caching = false # default 

    config.cache_store = :dalli_store, 'localhost:11211' 

我正在閱讀這個tutoria L於緩存:http://broadcastingadam.com/2012/07/advanced_caching_part_1-caching_strategies/

還有的例子,幾乎等同於我的情況給予適當的迴應:

Started GET "/posts/2" for 127.0.0.1 at 2011-05-01 16:54:43 -0700 
    Processing by PostsController#show as HTML 
    Parameters: {"id"=>"2"} 
Read fragment views/localhost:3000/posts/2 (0.5ms) 
Rendered posts/show.html.erb within layouts/application (6.1ms) 
Write fragment views/localhost:3000/posts/2 (0.5ms) 
Completed 200 OK in 16ms (Views: 8.6ms | ActiveRecord: 0.1ms) 

回答

0

OK,我只是測試了更改我的路線,並意識到問題是由於路線請求可選頁面ID的方式!

所以這是行不通的:

get "/clips/page(/:page)", :controller => "clips", :action => "index"

雖然這將工作:

get "/clips/page/:page", :controller => "clips", :action => "index"