Stores entity bodies on disk at the specified path.
Path where entities should be stored. This directory is created the first time the store is instansiated if it does not already exist.
# File lib/rack/cache/entitystore.rb, line 90 def initialize(root) @root = root FileUtils.mkdir_p root, :mode => 0755 end
# File lib/rack/cache/entitystore.rb, line 162 def self.resolve(uri) path = File.expand_path(uri.opaque || uri.path) new path end
# File lib/rack/cache/entitystore.rb, line 95 def exist?(key) File.exist?(body_path(key)) end
Open the entity body and return an IO object. The IO object’s each method is overridden to read 8K chunks instead of lines.
# File lib/rack/cache/entitystore.rb, line 116 def open(key) Body.open(body_path(key), 'rb') rescue Errno::ENOENT nil end
# File lib/rack/cache/entitystore.rb, line 140 def purge(key) File.unlink body_path(key) nil rescue Errno::ENOENT nil end
# File lib/rack/cache/entitystore.rb, line 99 def read(key) File.open(body_path(key), 'rb') { |f| f.read } rescue Errno::ENOENT nil end
# File lib/rack/cache/entitystore.rb, line 122 def write(body, ttl=nil) filename = ['buf', $$, Thread.current.object_id].join('-') temp_file = storage_path(filename) key, size = File.open(temp_file, 'wb') { |dest| slurp(body) { |part| dest.write(part) } } path = body_path(key) if File.exist?(path) File.unlink temp_file else FileUtils.mkdir_p File.dirname(path), :mode => 0755 FileUtils.mv temp_file, path end [key, size] end
# File lib/rack/cache/entitystore.rb, line 158 def body_path(key) storage_path spread(key) end
# File lib/rack/cache/entitystore.rb, line 152 def spread(key) key = key.dup key[2,0] = '/' key end
# File lib/rack/cache/entitystore.rb, line 148 def storage_path(stem) File.join root, stem end