我将 Rails (v3.2.21) 用于 API 应用程序,并尝试在响应中设置“Cache-Control” header ,以便我的客户端应用程序可以缓存它们。但是,实际响应中的 header 值仍然使用默认值(使用 Chrome 中的 Advanced Rest Client 插件检查)。
从谷歌看来,expires_in
方法是这样做的方法:
expires_in 3.hours, :public => true
但是,当我向资源发出请求时,它总是返回(默认?):
Cache-Control: must-revalidate, no-cache, no-store, private, max-age=0
当我在
expires_in
之后立即进行 binding.pry 操作时并检查 response.headers
,它是空的……似乎也错了。我在
before_filter
中执行上述操作在我的 Controller 中,并且在我调用 render
之前也尝试过回复我的回复。我应该补充一点,我已经尝试使用
response.headers["CacheControl"] = "blah"
手动设置“Cache-Control” header 但这也不起作用,我在别处读到 Rails 无论如何都不允许这样做。有没有人对我在这里做错了什么有任何想法?
最佳答案
这实际上是由于我在应用程序中的一些其他配置阻止了缓存的发生(特别是 Bullet.disable_browser_cache
设置,这对于开发环境是正确的)。将此设置为 false 意味着缓存正常工作。
关于ruby-on-rails - 在响应中设置 Cache-Control header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966357/