Concrete MetaStore implementation that stores request/response pairs on disk.
# File lib/rack/cache/metastore.rb, line 216 def initialize(root="/tmp/rack-cache/meta-#{ARGV[0]}") @root = File.expand_path(root) FileUtils.mkdir_p(root, :mode => 0755) end
# File lib/rack/cache/metastore.rb, line 259 def self.resolve(uri) path = File.expand_path(uri.opaque || uri.path) new path end
# File lib/rack/cache/metastore.rb, line 239 def purge(key) path = key_path(key) File.unlink(path) nil rescue Errno::ENOENT, IOError nil end
# File lib/rack/cache/metastore.rb, line 221 def read(key) path = key_path(key) File.open(path, 'rb') { |io| Marshal.load(io) } rescue Errno::ENOENT, IOError [] end
# File lib/rack/cache/metastore.rb, line 228 def write(key, entries) tries = 0 begin path = key_path(key) File.open(path, 'wb') { |io| Marshal.dump(entries, io, -1) } rescue Errno::ENOENT, IOError Dir.mkdir(File.dirname(path), 0755) retry if (tries += 1) == 1 end end
# File lib/rack/cache/metastore.rb, line 248 def key_path(key) File.join(root, spread(hexdigest(key))) end
# File lib/rack/cache/metastore.rb, line 252 def spread(sha, n=2) sha = sha.dup sha[n,0] = '/' sha end