class Geminabox::Geminabox::Geminabox::DiskCache

Attributes

root_path[R]

Public Class Methods

new(root_path) click to toggle source
# File lib/geminabox/disk_cache.rb, line 6
def initialize(root_path)
  @root_path = root_path
  ensure_dir_exists!
end

Public Instance Methods

cache(key) { || ... } click to toggle source
# File lib/geminabox/disk_cache.rb, line 21
def cache(key)
  key_hash = key_hash(key)
  read(key_hash) || write(key_hash, yield)
end
flush() click to toggle source
# File lib/geminabox/disk_cache.rb, line 16
def flush
  FileUtils.rm_rf(root_path)
  ensure_dir_exists!
end
flush_key(key) click to toggle source
# File lib/geminabox/disk_cache.rb, line 11
def flush_key(key)
  path = path(key_hash(key))
  FileUtils.rm_f(path)
end
marshal_cache(key) { || ... } click to toggle source
# File lib/geminabox/disk_cache.rb, line 26
def marshal_cache(key)
  key_hash = key_hash(key)
  marshal_read(key_hash) || marshal_write(key_hash, yield)
end

Protected Instance Methods

ensure_dir_exists!() click to toggle source
# File lib/geminabox/disk_cache.rb, line 33
def ensure_dir_exists!
  FileUtils.mkdir_p(root_path)
end
key_hash(key) click to toggle source
# File lib/geminabox/disk_cache.rb, line 37
def key_hash(key)
  Digest::MD5.hexdigest(key)
end
marshal_read(key_hash) click to toggle source
# File lib/geminabox/disk_cache.rb, line 49
def marshal_read(key_hash)
  read_int(key_hash) { |path| Marshal.load(File.open(path)) }
end
marshal_write(key_hash, value) click to toggle source
# File lib/geminabox/disk_cache.rb, line 63
def marshal_write(key_hash, value)
  write_int(key_hash) { |f| Marshal.dump(value, f) }
  value
end
path(key_hash) click to toggle source
# File lib/geminabox/disk_cache.rb, line 41
def path(key_hash)
  File.join(root_path, key_hash)
end
read(key_hash) click to toggle source
# File lib/geminabox/disk_cache.rb, line 45
def read(key_hash)
  read_int(key_hash) { |path| File.read(path) }
end
read_int(key_hash) { |path| ... } click to toggle source
# File lib/geminabox/disk_cache.rb, line 53
def read_int(key_hash)
  path = path(key_hash)
  yield(path) if File.exists?(path)
end
write(key_hash, value) click to toggle source
# File lib/geminabox/disk_cache.rb, line 58
def write(key_hash, value)
  write_int(key_hash) { |f| f << value }
  value
end
write_int(key_hash) { |f| ... } click to toggle source
# File lib/geminabox/disk_cache.rb, line 68
def write_int(key_hash)
  File.open(path(key_hash), 'wb') { |f| yield(f) }
end