2013-03-11 27 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版本開始)