Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

40 lines (31 sloc) 0.64 kB
require "fileutils"
class Geminabox::DiskCache
attr_reader :root_path
def initialize(root_path)
@root_path = root_path
ensure_dir_exists!
end
def flush
FileUtils.rm_rf(root_path)
ensure_dir_exists!
end
def cache(key)
key = Digest::MD5.hexdigest(key)
read(key) || write(key, yield)
end
def read(key)
path = File.join(root_path, key)
File.read(path) if File.exists?(path)
end
def write(key, value)
path = File.join(root_path, key)
File.open(path, 'wb'){|f|
f << value
}
value
end
protected
def ensure_dir_exists!
FileUtils.mkdir_p(root_path)
end
end
Jump to Line
Something went wrong with that request. Please try again.