22

我有一个基于 Ruby 和 Sinatra 的工作应用程序,它部署在 Heroku 上。

我想利用 Heroku 上可用的 HTTP 缓存,它使用 Varnish。

我不确定设置标题的最佳方法是什么,以及正确的语法。

关于最佳方法和语法的任何想法?

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
4

3 回答 3

31

通常动态生成的页面没有缓存,因此

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

标题是正确的起点。

尝试使用“使用基于 Web 的服务”中的一项服务,看看它们是否出现在从您的站点发回的 HTTPd 标头中。

于 2010-11-17T03:19:23.760 回答
1

您还可以使用以下语法访问响应对象的标头字段:

response['Cache-Control'] = 'public, max-age=600'
于 2010-11-18T07:11:51.283 回答
0

在 Sinatra 中,您可以使用以下cache_control方法:

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

  # Your magic goes here
end
于 2017-11-24T16:53:18.650 回答