我正在尝试在我的 Policy 控制器上实现动作缓存,但是,当呈现的片段大小增加时,它不会重新使用缓存的片段。
当我的操作“索引”仅显示10 个 JSON 对象时,它会正确创建和读取该操作的片段。证明:
Started GET "/policies" for 127.0.0.1 at 2013-03-10 01:03:38 -0500
Processing by PolicyController#index as JSON
Read fragment views/railsdev.com:3000/policies.json (0.3ms)
VideoPolicy Load (0.4ms) SELECT `video_policies`.* FROM `video_policies`
Write fragment views/railsdev.com:3000/policies.json (0.5ms)
Completed 200 OK in 27ms (Views: 0.2ms | ActiveRecord: 0.4ms)
Started GET "/policies" for 127.0.0.1 at 2013-03-10 01:03:56 -0500
Processing by PolicyController#index as JSON
Read fragment views/railsdev.com:3000/policies.json (0.4ms)
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
[2013-03-10 01:03:56] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
但是,当我将数量增加到10,000 个 JSON 对象时,它开始查询数据库并使用每个请求重新写入片段。证明:
Started GET "/policies" for 127.0.0.1 at 2013-03-10 01:12:45 -0500
Processing by PolicyController#index as JSON
Read fragment views/railsdev.com:3000/policies.json (0.3ms)
VideoPolicy Load (6.8ms) SELECT `video_policies`.* FROM `video_policies`
Write fragment views/railsdev.com:3000/policies.json (3.8ms)
Completed 200 OK in 3482ms (Views: 0.2ms | ActiveRecord: 7.5ms)
Started GET "/policies" for 127.0.0.1 at 2013-03-10 01:12:52 -0500
Processing by PolicyController#index as JSON
Read fragment views/railsdev.com:3000/policies.json (0.2ms)
VideoPolicy Load (1.7ms) SELECT `video_policies`.* FROM `video_policies`
Write fragment views/railsdev.com:3000/policies.json (115.0ms)
Completed 200 OK in 3713ms (Views: 0.2ms | ActiveRecord: 1.7ms)
[2013-03-10 01:12:55] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
我注意到当文件大小超过 1MB 标记时会发生这种情况(顺便说一句,这是 memcached 对象的最大大小。我也在使用 memcached。)是否存在某种最大片段大小或其他原因它没有缓存这些吗?
这也是我的控制器:
class PolicyController < ApplicationController
caches_action :index, :cache_path => Proc.new {|controller| controller.params }
def index
policies = VideoPolicy.all
respond_to do |format|
format.html
format.json{
render :json => policies.to_json
}
end
end
end