diff --git a/README.md b/README.md index 7b6a0ad..a547796 100644 --- a/README.md +++ b/README.md @@ -169,9 +169,6 @@ Billy.configure do |c| c.cache_path = 'spec/req_cache/' end -# need to call this because of a race condition between persist_cache -# being set and the proxy being loaded for the first time -Billy.proxy.restore_cache ``` `c.ignore_params` is used to ignore parameters of certain requests when diff --git a/lib/billy/cache.rb b/lib/billy/cache.rb index de4ba4c..030877c 100644 --- a/lib/billy/cache.rb +++ b/lib/billy/cache.rb @@ -6,7 +6,6 @@ module Billy class Cache def initialize reset - load_dir end def cacheable?(url, headers) @@ -18,11 +17,26 @@ def cacheable?(url, headers) end def cached?(method, url, body) - !@cache[key(method, url, body)].nil? + key = key(method, url, body) + !@cache[key].nil? or persisted?(key) + end + + def persisted?(key) + Billy.config.persist_cache and File.exists?(cache_file(key)) end def fetch(method, url, body) - @cache[key(method, url, body)] + key = key(method, url, body) + @cache[key] or fetch_from_persistence(key) + end + + def fetch_from_persistence(key) + begin + @cache[key] = YAML.load(File.open(cache_file(key))) if persisted?(key) + rescue ArgumentError => e + puts "Could not parse YAML: #{e.message}" + nil + end end def store(method, url, body, status, headers, content) @@ -35,15 +49,14 @@ def store(method, url, body, status, headers, content) :content => content } - @cache[key(method, url, body)] = cached + key = key(method, url, body) + @cache[key] = cached if Billy.config.persist_cache Dir.mkdir(Billy.config.cache_path) unless File.exists?(Billy.config.cache_path) begin - path = File.join(Billy.config.cache_path, - "#{key(method, url, body)}.yml") - File.open(path, 'w') do |f| + File.open(cache_file(key), 'w') do |f| f.write(cached.to_yaml(:Encoding => :Utf8)) end rescue StandardError => e @@ -55,20 +68,6 @@ def reset @cache = {} end - def load_dir - if Billy.config.persist_cache - Dir.glob(Billy.config.cache_path+"*.yml") { |filename| - data = begin - YAML.load(File.open(filename)) - rescue ArgumentError => e - puts "Could not parse YAML: #{e.message}" - end - - @cache[key(data[:method], data[:url], data[:body])] = data - } - end - end - def key(method, url, body) url = URI(url) no_params = url.scheme+'://'+url.host+url.path @@ -85,5 +84,9 @@ def key(method, url, body) key end + + def cache_file(key) + File.join(Billy.config.cache_path, "#{key}.yml") + end end -end +end \ No newline at end of file diff --git a/lib/billy/proxy.rb b/lib/billy/proxy.rb index a1ff715..395f9a0 100644 --- a/lib/billy/proxy.rb +++ b/lib/billy/proxy.rb @@ -54,8 +54,8 @@ def reset_cache end def restore_cache + warn "[DEPRECATION] `restore_cache` is deprecated as cache files are dynamincally checked. Use `reset_cache` if you just want to clear the cache." @cache.reset - @cache.load_dir end protected diff --git a/lib/billy/version.rb b/lib/billy/version.rb index f08ef71..809c783 100644 --- a/lib/billy/version.rb +++ b/lib/billy/version.rb @@ -1,3 +1,3 @@ module Billy - VERSION = "0.2.1" + VERSION = "0.2.2" end diff --git a/spec/lib/proxy_spec.rb b/spec/lib/proxy_spec.rb index d31c78f..a5283a7 100644 --- a/spec/lib/proxy_spec.rb +++ b/spec/lib/proxy_spec.rb @@ -121,6 +121,19 @@ r = http.get('/foo') File.exists?(cached_file).should be_true end + + it 'should be read initially from persistent cache' do + File.open(cached_file, 'w') do |f| + cached = { + :headers => {}, + :content => "GET /foo cached" + } + f.write(cached.to_yaml(:Encoding => :Utf8)) + end + + r = http.get('/foo') + r.body.should == 'GET /foo cached' + end end context "disabled" do