HTTP レスポンスキャッシュプラグイン
一度取得した HTTP レスポンスをローカルにキャッシュするプラグイン。
2 回目以降同じ URL に対してアクセスした場合は、
キャッシュしたローカルのファイルから HTTP レスポンスを取得します。
キャッシュファイルを消せば、新しい HTTP レスポンスを取得します。
Last-Modified ヘッダとかまったく考慮していません。
今のところはプラグイン開発のテスト中にサーバ側に負担をかけないためのプラグインです。
キャッシュファイルは、指定したディレクトリに URL を MD5 にした名前で保存しています。
サーバへのアクセスが主体になる Pragger だからこそ
Proxy 処理・設定は自前でもっていた方が楽なんだけれどね。
- plugin/Filter/http_cache.rb
## Force HTTP response cache -- gtaka555 ## ## Force HTTP response cache plugin. ## ## - module: Filter::http_cache ## config: ## cache: /home/opt/pragger/cache ## require 'md5' require 'fileutils' require 'open-uri' class HTTPCache def initialize(dir) @cache_dir = dir end def get(uri) return '' unless File.exist?(@cache_dir) && File.directory?(@cache_dir) cache_file = "#{@cache_dir}/#{Digest::MD5.new.update(uri.to_s)}.html" return '' unless File.file?(cache_file) && File.readable?(cache_file) text = '' File::open(cache_file) {|f| text = f.read } text end def set(uri, text) FileUtils.mkdir_p(@cache_dir) unless File.exist?(@cache_dir) return unless File.exist?(@cache_dir) && File.directory?(@cache_dir) cache_file = "#{@cache_dir}/#{Digest::MD5.new.update(uri.to_s)}.html" File::open(cache_file, "w") {|f| f.write(text) } return 0 end end def http_cache(config, data) unless config.key?('cache') data.map {|url| text = '' open(url) {|f| text = f.read } text.toutf8 } else cache = HTTPCache.new(config['cache']) data.map {|url| if url =~ %r{^http://} text = cache.get(url) if text.empty? text = '' open(url) {|f| text = f.read } cache.set(url, text) end text.toutf8 else text = '' open(url) {|f| text = f.read } text.toutf8 end } end end