23

I've got a working app based in Ruby and Sinatra that is deployed on Heroku.

I want to take advantage of the HTTP caching available on Heroku, which uses Varnish.

I'm not sure what the best way to set the headers is, and the correct syntax.

Any thoughts on the best approach and syntax?

before do
    headers "Content-Type" => "text/html; charset=utf8"
end

get '/' do
    headers['Cache-Control'] = 'public, max-age=600'

    # SOME STUFF HERE

    haml :home, {:layout => :layout_minfooter}

end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Neil
  • 297
  • 1
  • 2
  • 5

3 Answers3

32

Usually dynamically generated pages have no caching so the

response.headers['Cache-Control'] = 'public, max-age=300'

header is the right starting point.

Try using one of the services at "Use a Web-based service" to see if they show up in the HTTPd header sent back from your site.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
1

You can also access the header fields of the response object with this syntax:

response['Cache-Control'] = 'public, max-age=600'
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Michael Baker
  • 63
  • 1
  • 5
0

In Sinatra you can use the cache_control method:

get '/' do
  # Cache for 24 hours
  cache_control :public, max_age: 86400

  # Your magic goes here
end
MikeRogers0
  • 721
  • 6
  • 14