Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use Marshal as appropriate within cache data
Conflicts:

	lib/geminabox.rb
  • Loading branch information
jfoy committed Jan 19, 2013
1 parent 7077bd8 commit d6d5613
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/geminabox.rb
Expand Up @@ -54,7 +54,7 @@ def fixup_bundler_rubygems!

# Return a list of versions of gem 'gem_name' with the dependencies of each version.
def gem_dependencies(gem_name)
dependency_cache.cache(gem_name) do
dependency_cache.marshal_cache(gem_name) do
load_gems.select {|gem| gem_name == gem.name }.map do |gem|
spec = spec_for(gem.name, gem.number)
{
Expand Down
29 changes: 24 additions & 5 deletions lib/geminabox/disk_cache.rb
Expand Up @@ -23,6 +23,11 @@ def cache(key)
read(key_hash) || write(key_hash, yield)
end

def marshal_cache(key)
key_hash = key_hash(key)
marshal_read(key_hash) || marshal_write(key_hash, yield)
end

protected

def ensure_dir_exists!
Expand All @@ -38,16 +43,30 @@ def path(key_hash)
end

def read(key_hash)
read_int(key_hash) { |path| File.read(path) }
end

def marshal_read(key_hash)
read_int(key_hash) { |path| Marshal.load(File.open(path)) }
end

def read_int(key_hash)
path = path(key_hash)
File.read(path) if File.exists?(path)
yield(path) if File.exists?(path)
end

def write(key_hash, value)
path = path(key_hash)
File.open(path, 'wb'){|f|
f << value
}
write_int(key_hash) { |f| f << value }
value
end

def marshal_write(key_hash, value)
write_int(key_hash) { |f| Marshal.dump(value, f) }
value
end

def write_int(key_hash)
File.open(path(key_hash), 'wb') { |f| yield(f) }
end

end

0 comments on commit d6d5613

Please sign in to comment.