Class: Rack::Cache::MetaStore::Disk

Inherits:
Rack::Cache::MetaStore show all
Defined in:
lib/rack/cache/metastore.rb

Overview

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

Constant Summary

Constants inherited from Rack::Cache::MetaStore

DISK, FILE, GAE, GAECACHE, HEAP, MEM, MEMCACHE, MEMCACHED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rack::Cache::MetaStore

#cache_key, #invalidate, #lookup, #store

Constructor Details

#initialize(root = "/tmp/rack-cache/meta-#{ARGV[0]}") ⇒ Disk

Returns a new instance of Disk.



210
211
212
213
# File 'lib/rack/cache/metastore.rb', line 210

def initialize(root="/tmp/rack-cache/meta-#{ARGV[0]}")
  @root = File.expand_path(root)
  FileUtils.mkdir_p(root, :mode => 0755)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



208
209
210
# File 'lib/rack/cache/metastore.rb', line 208

def root
  @root
end

Class Method Details

.resolve(uri) ⇒ Object



253
254
255
256
# File 'lib/rack/cache/metastore.rb', line 253

def self.resolve(uri)
  path = File.expand_path(uri.opaque || uri.path)
  new path
end

Instance Method Details

#purge(key) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/rack/cache/metastore.rb', line 233

def purge(key)
  path = key_path(key)
  File.unlink(path)
  nil
rescue Errno::ENOENT, IOError
  nil
end

#read(key) ⇒ Object



215
216
217
218
219
220
# File 'lib/rack/cache/metastore.rb', line 215

def read(key)
  path = key_path(key)
  File.open(path, 'rb') { |io| Marshal.load(io) }
rescue Errno::ENOENT, IOError
  []
end

#write(key, entries) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'lib/rack/cache/metastore.rb', line 222

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