2012-08-11 71 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"