天天看點

varnish 清理緩存 purge和ban

【軟體版本】

varnish 4.x

參考:https://www.varnish-cache.org/docs/4.1/users-guide/purging.html

【分析purge 和 ban的差別】

共同點:

清除緩存

不同點:

purge 是一個動作 執行對通路的url在緩存中進行purge 清除。 一次隻能對一個url

ban 是一個政策,必須先增加ban禁止政策,下次通路比對到這個禁止政策的時候,不會去緩存中找,直接去fetch後面的。ban能清理比對到的任何内容。

purge比較好了解,ban看官網的一段解釋

官方原文:

There is another way to invalidate content: Bans. You can think of bans as a sort of a filter on objects already in the cache. You <code>ban</code>certain content from being served from your cache. You can ban content based on any metadata we have. A ban will only work on objects already in the cache, it does not prevent new content from entering the cache or being served.

Support for bans is built into Varnish and available in the CLI interface. To ban every png object belonging on example.com, issue the following command:

翻譯:

這個是(清除)無效内容的另一個方法: Bans. 你能将bans作為一個排序過的過濾緩存中的對象。你禁止某些内容來自你的緩存中。你能禁止的内容是我們緩存中的任何中繼資料。ban 僅僅工作在已經緩存的對象上,它不能阻止一個新的内容條目将被緩存。(我了解的意思是 ban隻能使用一次,即一次性清理)

【用法】

purge的用法:

1 vcl配置檔案中添加purge允許

2 curl -I - X PURGE http://10.59.74.33/123.jpg

ban的用法:

添加一個ban到禁止list中。

1 varnishadm -S /etc/varnish/secret -T 127.0.0.1:8080 'ban req.url ~ "0*.jpg$"'

當通路任何0*.jpg的uri的時候,将不會從緩存中取,直接去後端取(其實就是禁止使用緩存,換句話就是達到了 清除緩存的目的,這個就是squid沒有設計到的,因為squid的清理隻有purge 隻能是執行動作去清理(一個很大的目錄需要很多cpu),而不是想辦法去比對到禁止的url然後不去進入緩存中去取,達到相同的目的。)

本文轉自殘劍部落格51CTO部落格,原文連結http://blog.51cto.com/cuidehua/1867431如需轉載請自行聯系原作者

cuizhiliang

繼續閱讀