0

我试图理解为什么我的 Rails 动作缓存没有基于正确的 URL 请求和参数进行缓存。

例如,当我请求时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,而响应是 "fragment" 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 配置文件,我正在测试它已启用缓存,如下所示:

  # 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)
4

1 回答 1

0

好的,我刚刚测试了对路线的更改,并意识到问题是由于路线要求可选页面 id 的方式造成的!

所以这不起作用:

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

虽然这会起作用:

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

于 2012-08-11T21:02:02.413 回答