Module: Opal::Cache

Defined in:
opal/lib/opal/cache.rb,
opal/lib/opal/cache/file_cache.rb

Defined Under Namespace

Classes: FileCache, NullCache

Class Method Summary collapse

Class Method Details

.digest(string) ⇒ Object



67
68
69
# File 'opal/lib/opal/cache.rb', line 67

def digest(string)
  ::Digest::SHA256.hexdigest(string)[-32..-1].to_i(16).to_s(36)
end

.fetch(cache, key, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'opal/lib/opal/cache.rb', line 33

def fetch(cache, key, &block)
  # Extension to the Sprockets API of Cache, if a cache responds
  # to #fetch, then we call it instead of using #get and #set.
  return cache.fetch(key, &block) if cache.respond_to? :fetch

  key = digest(key.join('/')) + '-' + runtime_key

  data = cache.get(key)

  data || begin
            compiler = yield
            cache.set(key, compiler)
            compiler
          end
end

.runtime_keyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'opal/lib/opal/cache.rb', line 49

def runtime_key
  @runtime_key ||= begin
    # We want to ensure the compiler and any Gemfile/gemspec (for development)
    # stays untouched
    opal_path = File.expand_path('..', Opal.gem_dir)
    files = Dir["#{opal_path}/{Gemfile*,*.gemspec,lib/**/*}"]

    # Also check if parser wasn't changed:
    files += $LOADED_FEATURES.grep(%r{lib/(parser|ast)})

    digest [
      files.sort.map { |f| "#{f}:#{File.size(f)}:#{File.mtime(f).to_f}" },
      RUBY_VERSION,
      RUBY_PATCHLEVEL
    ].join('/')
  end
end