2013-03-11 31 views
2

我有一个Sinatra路径来显示状态图像。虽然这种简单的解决方案的工作,我碰到缓存问题:将重定向缓存到Sinatra的静态图像

get '/stream/:service/:stream_id.png' do 
    # Building image_url omitted 

    redirect image_url 
end 

什么是这里处理缓存,设置一个最大TTL有道?这些图像将嵌入其他网站,否则我可以直接链接到我重定向到的图像。

问题是它会生成一个像site.com/image.png这样的URL,然后将其重定向到其他地方 - 但它是site.com/image.png,它被浏览器缓存,因此它不会检查它是否已更新。

我已经尝试了一下Cache-Control头文件,但是我还没有找到解决方案。

如果此方法完全过时,我可以开放其他解决方案。

回答

2

您设置的Cache-Control每路径基础:

get '/stream/:service/:stream_id.png' do 
    # Building image_url omitted 
    response['Cache-Control'] = "public, max-age=0, must-revalidate" 
    redirect image_url 
end 
0

您还可以使用Sinatra的expires方法:

# Set the Expires header and Cache-Control/max-age directive. Amount 
# can be an integer number of seconds in the future or a Time object 
# indicating when the response should be considered "stale". The remaining 
# "values" arguments are passed to the #cache_control helper: 
# 
# expires 500, :public, :must_revalidate 
# => Cache-Control: public, must-revalidate, max-age=60 
# => Expires: Mon, 08 Jun 2009 08:50:17 GMT 

还是cache_control方法:

# Specify response freshness policy for HTTP caches (Cache-Control header). 
# Any number of non-value directives (:public, :private, :no_cache, 
# :no_store, :must_revalidate, :proxy_revalidate) may be passed along with 
# a Hash of value directives (:max_age, :min_stale, :s_max_age). 
# 
# cache_control :public, :must_revalidate, :max_age => 60 
# => Cache-Control: public, must-revalidate, max-age=60 
# 
# See RFC 2616/14.9 for more on standard cache control directives: 
# http://tools.ietf.org/html/rfc2616#section-14.9.1 

Sinatra文档(从1.4.6版本开始)