尝试使用memcached缓存Rails多页面视图

时间:2012-08-11 20:56:27

标签: ruby-on-rails-3 caching memcached

我试图理解为什么我的Rails操作缓存不是基于正确的URL请求进行缓存的& PARAMS。

例如,当我请求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)

我认为奇怪的是,显然要求的网址是/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'

我正在阅读有关缓存的教程: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)

1 个答案:

答案 0 :(得分:0)

好的,我刚测试了我的路线的变化,并意识到问题是由于路线要求可选页面ID的方式!

所以这不起作用:

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

虽然这样可行:

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