class Rack::Cache::MetaStore::Disk

Concrete MetaStore implementation that stores request/response pairs on disk.

Attributes

root[R]

Public Class Methods

new(root="/tmp/rack-cache/meta- click to toggle source
# File lib/rack/cache/meta_store.rb, line 240
def initialize(root="/tmp/rack-cache/meta-#{ARGV[0]}")
  @root = File.expand_path(root)
  FileUtils.mkdir_p(root, :mode => 0755)
end
resolve(uri) click to toggle source
# File lib/rack/cache/meta_store.rb, line 283
def self.resolve(uri)
  path = File.expand_path(uri.opaque || uri.path)
  new path
end

Public Instance Methods

purge(key) click to toggle source
# File lib/rack/cache/meta_store.rb, line 263
def purge(key)
  path = key_path(key)
  File.unlink(path)
  nil
rescue Errno::ENOENT, IOError
  nil
end
read(key) click to toggle source
# File lib/rack/cache/meta_store.rb, line 245
def read(key)
  path = key_path(key)
  File.open(path, 'rb') { |io| Marshal.load(io) }
rescue Errno::ENOENT, IOError
  []
end
write(key, entries, ttl = nil) click to toggle source
# File lib/rack/cache/meta_store.rb, line 252
def write(key, entries, ttl = nil)
  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

Private Instance Methods

key_path(key) click to toggle source
# File lib/rack/cache/meta_store.rb, line 272
def key_path(key)
  File.join(root, spread(hexdigest(key)))
end
spread(sha, n=2) click to toggle source
# File lib/rack/cache/meta_store.rb, line 276
def spread(sha, n=2)
  sha = sha.dup
  sha[n,0] = '/'
  sha
end