class Rack::Cache::EntityStore::FILE

Stores entity bodies on disk at the specified path.

Attributes

root[R]

Path where entities should be stored. This directory is created the first time the store is instansiated if it does not already exist.

Public Class Methods

new(root) click to toggle source
# File lib/rack/cache/entitystore.rb, line 90
def initialize(root)
  @root = root
  FileUtils.mkdir_p root, :mode => 0755
end

Protected Class Methods

resolve(uri) click to toggle source
# File lib/rack/cache/entitystore.rb, line 162
def self.resolve(uri)
  path = File.expand_path(uri.opaque || uri.path)
  new path
end

Public Instance Methods

exist?(key) click to toggle source
# File lib/rack/cache/entitystore.rb, line 95
def exist?(key)
  File.exist?(body_path(key))
end
open(key) click to toggle source

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
purge(key) click to toggle source
# File lib/rack/cache/entitystore.rb, line 140
def purge(key)
  File.unlink body_path(key)
  nil
rescue Errno::ENOENT
  nil
end
read(key) click to toggle source
# 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
write(body, ttl=nil) click to toggle source
# 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

Protected Instance Methods

body_path(key) click to toggle source
# File lib/rack/cache/entitystore.rb, line 158
def body_path(key)
  storage_path spread(key)
end
spread(key) click to toggle source
# File lib/rack/cache/entitystore.rb, line 152
def spread(key)
  key = key.dup
  key[2,0] = '/'
  key
end
storage_path(stem) click to toggle source
# File lib/rack/cache/entitystore.rb, line 148
def storage_path(stem)
  File.join root, stem
end