Sha256: 803e78ae58396a321281122c6cc03b739a7c561e70f5265f266c4378b4b6aeda

Contents?: true

Size: 640 Bytes

Versions: 4

Compression:

Stored size: 640 Bytes

Contents

require "fileutils"

class Geminabox::DiskCache
  attr_reader :root_path

  def initialize(root_path)
    @root_path = root_path
    ensure_dir_exists!
  end

  def flush
    FileUtils.rm_rf(root_path)
    ensure_dir_exists!
  end

  def cache(key)
    key = Digest::MD5.hexdigest(key)
    read(key) || write(key, yield)
  end

  def read(key)
    path = File.join(root_path, key)
    File.read(path) if File.exists?(path)
  end

  def write(key, value)
    path = File.join(root_path, key)
    File.open(path, 'wb'){|f|
      f << value
    }
    value
  end

protected

  def ensure_dir_exists!
    FileUtils.mkdir_p(root_path)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geminabox-0.8.0 lib/geminabox/disk_cache.rb
geminabox-0.7.0 lib/geminabox/disk_cache.rb
geminabox-0.6.1 lib/geminabox/disk_cache.rb
geminabox-0.6.1pre1 lib/geminabox/disk_cache.rb