David Elner
David Elner

Reputation: 5191

Action caching is not re-using large cached fragments

I'm attempting to implement action caching on my Policy controller, however, it isn't re-using cached fragments when the rendered fragment grows in size.

When my action 'index' shows just 10 JSON objects, it creates and reads fragments for the action properly. Evidenced by:

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

However, when I increase the number to 10,000 JSON objects, it begins to query the DB and re-write the fragment with each request. Evidenced by:

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

I've noticed this happens when the file size breaks the 1MB mark (which is co-incidentally the maximum size of a memcached object. I am also using memcached.) Is there some sort of max fragment-size, or other reason for why it isn't caching these?

Here is my controller as well:

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

Upvotes: 0

Views: 132

Answers (1)

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

I am guessing that it is the default page slab size of memcached that is causing this.

This discussion talks about this problem and some steps.

Memcahced man page says -I <size> can be used to override the default 1 mb. Can you try setting it to say 2mb and see if your app's behaviour changes?

Upvotes: 1

Related Questions