Sha256: cbeb103137795f4bbf6b9107548be8c24775755290a82a4a8e30c3604149573e

Contents?: true

Size: 1.99 KB

Versions: 8

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require "fileutils"
module Geminabox
  class Geminabox::DiskCache
    attr_reader :root_path

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

    def flush_key(key)
      path = path(key_hash(key))
      FileUtils.rm_f(path)
    end

    def flush
      FileUtils.rm_rf(root_path)
      ensure_dir_exists!
    end

    def cache(key)
      key_hash = key_hash(key)
      read(key_hash) || write(key_hash, yield)
    end

    def marshal_cache(key)
      key_hash = key_hash(key)
      marshal_read(key_hash) || marshal_write(key_hash, yield)
    end

  protected

    def ensure_dir_exists!
      FileUtils.mkdir_p(root_path)
    end

    def key_hash(key)
      Digest::MD5.hexdigest(key)
    end

    def path(key_hash)
      File.join(root_path, key_hash)
    end

    def read(key_hash)
      read_int(key_hash) do |path|
        begin
          File.read(path)
        rescue Errno::ENOENT
          # There is a possibility that the file is removed by another process
          # after checking File.exist?. Return nil if the file does not exist.
          nil
        end
      end
    end

    def marshal_read(key_hash)
      read_int(key_hash) do |path|
        begin
          File.open(path) {|fp| Marshal.load(fp) }
        rescue Errno::ENOENT, EOFError
          # There is a possibility that the file is removed by another process.
          # Marshal.load raises EOFError if the file is removed after File.open(path) succeeds.
          # Return nil if the file does not exist.
          nil
        end
      end
    end

    def read_int(key_hash)
      path = path(key_hash)
      yield(path) if File.exist?(path)
    end

    def write(key_hash, value)
      write_int(key_hash) { |f| f << value }
      value
    end

    def marshal_write(key_hash, value)
      write_int(key_hash) { |f| Marshal.dump(value, f) }
      value
    end

    def write_int(key_hash)
      File.open(path(key_hash), 'wb') { |f| yield(f) }
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
geminabox-2.1.0 lib/geminabox/disk_cache.rb
geminabox-2.1.0.rc.1 lib/geminabox/disk_cache.rb
geminabox-2.0.0 lib/geminabox/disk_cache.rb
geminabox-2.0.0.rc.1 lib/geminabox/disk_cache.rb
geminabox-1.5.0 lib/geminabox/disk_cache.rb
geminabox-1.5.0.rc.1 lib/geminabox/disk_cache.rb
geminabox-1.4.3 lib/geminabox/disk_cache.rb
geminabox-1.4.3.rc lib/geminabox/disk_cache.rb