Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for existing persistent cache files on demand #26

Merged
merged 6 commits into from
Dec 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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