Sha256: 8b8d495a098d7751c29d213b8da3c8d3ff6116c2e8bd8c506fb41a6aefc13e40

Contents?: true

Size: 808 Bytes

Versions: 2

Compression:

Stored size: 808 Bytes

Contents

module Bunch
  module Caching
    class << self
      def initialize_cache
        @cache = Hash.new { |h, k| h[k] = {} }
      end

      def stored?(fn)
        @cache.keys.include?(fn)
      end

      def read(fn)
        @cache[fn][:content]
      end

      def mtime(fn)
        @cache[fn][:mtime]
      end

      def write(fn, mtime, content)
        @cache[fn] = {:mtime => mtime, :content => content}
      end

      def fetch(fn, &blk)
        current_mtime = File.mtime(fn)

        if stored?(fn) && mtime(fn) == current_mtime
          read(fn)
        else
          content = blk.call
          write(fn, current_mtime, content)
          content
        end
      end
    end
    initialize_cache

    def fetch(filename, &blk)
      Bunch::Caching.fetch(filename, &blk)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bunch-0.0.6 lib/bunch/caching.rb
bunch-0.0.5 lib/bunch/caching.rb