Class: Evilution::SourceAstCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/evilution/source_ast_cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Content-hash-keyed LRU of Prism::ParseResult. Different source bytes always yield a different key, so the cache is valid for the lifetime of the process.

Instance Method Summary collapse

Constructor Details

#initialize(max_entries: DEFAULT_MAX_ENTRIES) ⇒ SourceAstCache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SourceAstCache.



13
14
15
16
# File 'lib/evilution/source_ast_cache.rb', line 13

def initialize(max_entries: DEFAULT_MAX_ENTRIES)
  @max_entries = max_entries
  @entries = {}
end

Instance Method Details

#fetch(source) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/evilution/source_ast_cache.rb', line 18

def fetch(source)
  return Prism.parse(source) if @max_entries <= 0

  key = Digest::SHA256.digest(source)
  if @entries.key?(key)
    result = @entries.delete(key)
    @entries[key] = result
    return result
  end

  result = Prism.parse(source)
  @entries[key] = result
  evict_until_within_bounds
  result
end