From 35ca1ba6d244738525c73d2d31606e97491ac119 Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 01:12:10 -0400 Subject: [PATCH 1/6] Fetch from cached file initially --- lib/billy/cache.rb | 23 +++++++++++++++++------ spec/lib/proxy_spec.rb | 13 +++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/billy/cache.rb b/lib/billy/cache.rb index de4ba4c..c401820 100644 --- a/lib/billy/cache.rb +++ b/lib/billy/cache.rb @@ -18,11 +18,22 @@ def cacheable?(url, headers) end def cached?(method, url, body) - !@cache[key(method, url, body)].nil? + !@cache[key(method, url, body)].nil? or persisted?(method, url, body) + end + + def persisted?(method, url, body) + Billy.config.persist_cache and File.exists?(File.join(Billy.config.cache_path, "#{key(method, url, body)}.yml")) end def fetch(method, url, body) - @cache[key(method, url, body)] + @cache[key(method, url, body)] or fetch_from_persistence(method, url, body) + end + + def fetch_from_persistence(method, url, body) + if Billy.config.persist_cache and Billy.config.cache_path + cache_file = File.join(Billy.config.cache_path, "#{key(method, url, body)}.yml") + YAML.load(File.open(cache_file)) + end end def store(method, url, body, status, headers, content) @@ -59,10 +70,10 @@ 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 + 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 } 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 From c7121dd821d0d96a7da0b12421c9119c5d168058 Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 01:18:53 -0400 Subject: [PATCH 2/6] Removed duplication in generating cache key --- lib/billy/cache.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/billy/cache.rb b/lib/billy/cache.rb index c401820..348b672 100644 --- a/lib/billy/cache.rb +++ b/lib/billy/cache.rb @@ -18,20 +18,22 @@ def cacheable?(url, headers) end def cached?(method, url, body) - !@cache[key(method, url, body)].nil? or persisted?(method, url, body) + key = key(method, url, body) + !@cache[key].nil? or persisted?(key) end - def persisted?(method, url, body) - Billy.config.persist_cache and File.exists?(File.join(Billy.config.cache_path, "#{key(method, url, body)}.yml")) + def persisted?(key) + Billy.config.persist_cache and File.exists?(File.join(Billy.config.cache_path, "#{key}.yml")) end def fetch(method, url, body) - @cache[key(method, url, body)] or fetch_from_persistence(method, url, body) + key = key(method, url, body) + @cache[key] or fetch_from_persistence(key) end - def fetch_from_persistence(method, url, body) + def fetch_from_persistence(key) if Billy.config.persist_cache and Billy.config.cache_path - cache_file = File.join(Billy.config.cache_path, "#{key(method, url, body)}.yml") + cache_file = File.join(Billy.config.cache_path, "#{key}.yml") YAML.load(File.open(cache_file)) end end @@ -46,14 +48,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") + path = File.join(Billy.config.cache_path, "#{key}.yml") File.open(path, 'w') do |f| f.write(cached.to_yaml(:Encoding => :Utf8)) end From d344005faa931d2086aaa6227c47b0aa621982be Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 01:38:56 -0400 Subject: [PATCH 3/6] Removed cache#load_dir as loading files is now done dynamically --- lib/billy/cache.rb | 15 --------------- lib/billy/proxy.rb | 5 +---- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/lib/billy/cache.rb b/lib/billy/cache.rb index 348b672..e5803d1 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) @@ -68,20 +67,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 diff --git a/lib/billy/proxy.rb b/lib/billy/proxy.rb index a1ff715..24654c4 100644 --- a/lib/billy/proxy.rb +++ b/lib/billy/proxy.rb @@ -53,10 +53,7 @@ def reset_cache @cache.reset end - def restore_cache - @cache.reset - @cache.load_dir - end + alias_method :restore_cache, :reset_cache protected From 07b6e0fb96e16aee58e21dec557a64c09097910a Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 02:01:40 -0400 Subject: [PATCH 4/6] Refactored generation of cache file and story cached content into cache when fetched --- lib/billy/cache.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/billy/cache.rb b/lib/billy/cache.rb index e5803d1..030877c 100644 --- a/lib/billy/cache.rb +++ b/lib/billy/cache.rb @@ -22,7 +22,7 @@ def cached?(method, url, body) end def persisted?(key) - Billy.config.persist_cache and File.exists?(File.join(Billy.config.cache_path, "#{key}.yml")) + Billy.config.persist_cache and File.exists?(cache_file(key)) end def fetch(method, url, body) @@ -31,9 +31,11 @@ def fetch(method, url, body) end def fetch_from_persistence(key) - if Billy.config.persist_cache and Billy.config.cache_path - cache_file = File.join(Billy.config.cache_path, "#{key}.yml") - YAML.load(File.open(cache_file)) + 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 @@ -54,8 +56,7 @@ def store(method, url, body, status, headers, content) Dir.mkdir(Billy.config.cache_path) unless File.exists?(Billy.config.cache_path) begin - path = File.join(Billy.config.cache_path, "#{key}.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 @@ -83,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 From 3755956fb3c97f03154616fb360bdce7e9a7029c Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 10:46:19 -0400 Subject: [PATCH 5/6] Added deprecation for Billy.proxy.restore_cache --- README.md | 3 --- lib/billy/proxy.rb | 5 ++++- 2 files changed, 4 insertions(+), 4 deletions(-) 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/proxy.rb b/lib/billy/proxy.rb index 24654c4..395f9a0 100644 --- a/lib/billy/proxy.rb +++ b/lib/billy/proxy.rb @@ -53,7 +53,10 @@ def reset_cache @cache.reset end - alias_method :restore_cache, :reset_cache + 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 + end protected From cb0f7eef7b5a55cae7d2652a61ed764f268a8b98 Mon Sep 17 00:00:00 2001 From: Declan Whelan Date: Sat, 24 Aug 2013 12:30:38 -0400 Subject: [PATCH 6/6] Version bump --- lib/billy/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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