Class: Rubyzen::Cache::ParseCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyzen/cache/parse_cache.rb

Overview

In-memory cache for parsed AST results, keyed by file path and SHA256 checksum. Automatically invalidates entries when file contents change.

Instance Method Summary collapse

Constructor Details

#initializeParseCache

Returns a new instance of ParseCache.



8
9
10
# File 'lib/rubyzen/cache/parse_cache.rb', line 8

def initialize
  @cache = {}
end

Instance Method Details

#fetch_or_parse(file_path) { ... } ⇒ Object

Returns the cached result for the given file, or yields to parse and cache it.

Parameters:

  • file_path (String)

    absolute path to the file

Yields:

  • block that parses the file and returns the result to cache

Returns:

  • (Object)

    the cached or freshly parsed result



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubyzen/cache/parse_cache.rb', line 17

def fetch_or_parse(file_path, &block)
  checksum = file_checksum(file_path)

  if @cache.key?(file_path) && @cache[file_path][:checksum] == checksum
    @cache[file_path][:result]
  else
    result = block.call
    @cache[file_path] = { checksum: checksum, result: result }
    result
  end
end