缓存控制标头和 Heroku 动态图像
我正在尝试了解 Heroku 上的 HTTP 缓存。阅读 他们的文章后,我很好奇 Cache-Control HTTP 标头是如何工作的。
在示例应用程序中提到的中文章标题在控制器操作中设置:
def image
@qrimage = QRImage.find_by_md5(params[:md5])
if @qrimage
headers['Cache-Control'] = 'public; max-age=2592000' # cache image for a month
send_data @qrimage.data, :filename => @qrimage.filename, :disposition => 'inline', :type => "image/png"
else
render :nothing => true, :status => 404
end
end
代码@qrimage.data
就像:
def data
qrcode = RQRCode::QRCode.new(self.message, :size => self.version, :level => self.ecc.to_sym)
qrcode.to_s
end
所以对我来说,看起来图像每次都是在服务器上生成的。然后被浏览器缓存一个月。因此,唯一的节省是当同一访问者尝试查看相同的图像时。
如果不同的访问者尝试查看同一图像,该图像仍会生成并发送。如果你问我的话,我并没有多大帮助。
我的理解是否正确,或者不会为每个站点访问者重新生成相同的图像?
I'm trying to understand HTTP Caching on Heroku. After reading their article, I'm curious about how the Cache-Control HTTP header is working.
In the sample application mentioned in the article the header is set in a controller action:
def image
@qrimage = QRImage.find_by_md5(params[:md5])
if @qrimage
headers['Cache-Control'] = 'public; max-age=2592000' # cache image for a month
send_data @qrimage.data, :filename => @qrimage.filename, :disposition => 'inline', :type => "image/png"
else
render :nothing => true, :status => 404
end
end
The code for @qrimage.data
is like:
def data
qrcode = RQRCode::QRCode.new(self.message, :size => self.version, :level => self.ecc.to_sym)
qrcode.to_s
end
So to me it looks like the image is being generated on the server every time. And then cached by the browser for a month. So the only savings here is when the same visitor tries to view the same image.
If different visitors try to view the same image, it will still be generated and sent. Not really all that helpful if you ask me.
Is my understanding correct, or will the same image not be regenerate for each site visitor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不知道,Varnish 本质上是一个非常快的缓存,位于您的应用程序和互联网之间。当标头表明可以安全缓存时,Varnish 就会执行此操作并使用缓存对象响应其他请求,而不会影响您的应用程序。
If you don't know, Varnish is a really fast cache that sits between your application and the internet, essentially. When headers say it's safe to cache, Varnish does so and responds to additional requests with the cached object without ever hitting your application.