Class: WhyClasses::Cache

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

Overview

An optional on-disk cache of analysis results, keyed by file content so a large repo can be re-scanned quickly when little has changed. Only used in report modes (never when applying corrections). Correctors are not serialized, so cached offenses render but are never re-applied from cache.

Instance Method Summary collapse

Constructor Details

#initialize(dir:, config_digest:, rule_names:) ⇒ Cache

Returns a new instance of Cache.



15
16
17
18
19
# File 'lib/why_classes/cache.rb', line 15

def initialize(dir:, config_digest:, rule_names:)
  @dir = dir
  @salt = [WhyClasses::VERSION, config_digest, rule_names.sort.join(",")].join("")
  FileUtils.mkdir_p(@dir)
end

Instance Method Details

#fetch(source_file) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/why_classes/cache.rb', line 21

def fetch(source_file)
  path = entry_path(source_file)
  return nil unless File.file?(path)

  deserialize(JSON.parse(File.read(path)))
rescue JSON::ParserError
  nil
end

#store(source_file, offenses) ⇒ Object



30
31
32
33
# File 'lib/why_classes/cache.rb', line 30

def store(source_file, offenses)
  File.write(entry_path(source_file), JSON.generate(offenses.map { |o| serialize(o) }))
  offenses
end