Sha256: db039dd5371f8e736f7f5c00c53bfc08ee11f1a689d9995d549e94e52f1f81f9

Contents?: true

Size: 799 Bytes

Versions: 1

Compression:

Stored size: 799 Bytes

Contents

# encoding: UTF-8

require "thread"

module Bunch
  def self.SimpleCache(*args)
    SimpleCache.new(*args)
  end

  class SimpleCache
    Result = Struct.new(:result)

    def initialize(processor_class, *args)
      @processor_class, @args = processor_class, args
      @cache = nil
      @hache = nil
      @mutex = Mutex.new
    end

    def new(tree)
      result = nil
      @mutex.synchronize do
        check_cache!(tree)
        @cache ||= begin
          processor = @processor_class.new(tree, *@args)
          processor.result
        end
        result = @cache.dup
      end
      Result.new(result)
    end

    private

    def check_cache!(tree)
      hash = ContentHash.new(tree).result
      if hash != @hache
        @cache = nil
        @hache = hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunch-1.0.0pre3 lib/bunch/simple_cache.rb