rack-cache icon indicating copy to clipboard operation
rack-cache copied to clipboard

Using Rack::Sendfile

Open ramontayag opened this issue 14 years ago • 3 comments

I've been stuck trying to get Rack::Cache to use Rack::Sendfile without any luck. I've sent a message to the mailing list but it seems that there's no activity there.

The details are in this StackOverflow question, but in summary, even with Rack::Sendfile in front of Rack::Cache, my images don't seem to be sent using Rack::Sendfile.

What do I do?

ramontayag avatar Nov 04 '11 17:11 ramontayag

This is how I insert the middleware:

Rails.application.middleware.insert 0, Rack::Sendfile
Rails.application.middleware.insert 1, Rack::Cache, {
  :verbose     => true,
  :metastore   => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"),
  :entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body")
} unless rack_cache_already_inserted

I then opened the Rack gem and played around with sendfile.rb to see if the body being returned by Rack::Cache responds to to_path, but it doesn't.

ramontayag avatar Nov 05 '11 03:11 ramontayag

After more investigating, I found out that Rails 3.1 (which I am on) already inserts Rack::Cache for you in production. This poses a problem though, as the configuration looks like this:

{:metastore=>"rails:/", :entitystore=>"rails:/", :verbose=>true}

I did some puts in the Rack::Cache source, and it seems none of the Entitystores are used. I know for Rack::Sendfile to work, the Disk class must be used as the entitystore.

Now I just need to find a way to use the disk store. I'm don't know where to set these at the moment.

ramontayag avatar Nov 05 '11 06:11 ramontayag

I ended up getting this to work, albeit with nginx & unicorn rather than Apache and Passenger.

You were almost there with your example above, changing the Rack::Cache stores to file:/ rather than rails:/, however you were missing the config.action_dispatch.x_sendfile_header argument to the Rack::Sendfile line. Without that Rack::Sendfile doesn't know which header to send back so doesn't send anything.

This Gist shows my nginx config template and relevant lines in production.rb

scottatron avatar Sep 16 '12 05:09 scottatron