Skip to content

Commit

Permalink
Merge pull request #26 from dwhelan/persistence
Browse files Browse the repository at this point in the history
Check for existing persistent cache files on demand
  • Loading branch information
oesmith committed Dec 15, 2013
2 parents 838b2be + cb0f7ee commit b0d9ec8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 25 additions & 22 deletions lib/billy/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Billy
class Cache
def initialize
reset
load_dir
end

def cacheable?(url, headers)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/billy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/billy/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Billy
VERSION = "0.2.1"
VERSION = "0.2.2"
end
13 changes: 13 additions & 0 deletions spec/lib/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0d9ec8

Please sign in to comment.