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

Public Instance Methods

purge(key) click to toggle source
    # File lib/rack/cache/meta_store.rb
263 def purge(key)
264   path = key_path(key)
265   File.unlink(path)
266   nil
267 rescue Errno::ENOENT, IOError
268   nil
269 end
read(key) click to toggle source
    # File lib/rack/cache/meta_store.rb
245 def read(key)
246   path = key_path(key)
247   File.open(path, 'rb') { |io| Marshal.load(io) }
248 rescue Errno::ENOENT, IOError
249   []
250 end
write(key, entries, ttl = nil) click to toggle source
    # File lib/rack/cache/meta_store.rb
252 def write(key, entries, ttl = nil)
253   tries = 0
254   begin
255     path = key_path(key)
256     File.open(path, 'wb') { |io| Marshal.dump(entries, io, -1) }
257   rescue Errno::ENOENT, IOError
258     Dir.mkdir(File.dirname(path), 0755)
259     retry if (tries += 1) == 1
260   end
261 end

Private Instance Methods

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